TextContent is equivalent to innerText in Firefox.

Source: Internet
Author: User

In IE and Opear, DOM objects support the innerText attribute and can easily remove HTML tags.
However, Firefox does not support this attribute. Fortunately, DOM objects in FF support textContent, which is equivalent to innerText.

Demo instance:
<P id = "TestObj"> Hi, I'm <strong> cnlei </strong>. welcome to my homepage: <a href = "http://www.cnlei.com"> http://www.cnlei.com </a>. </p>
<Script type = "text/javascript">
Function getInnerText (obj ){
Return document. all? Obj. innerText: obj. textContent;
}
Var str = getInnerText (document. getElementById ("TestObj "));
Alert (str );
</Script>

Add the innerText attribute to the DOM object in Firefox:
<Script type = "text/javascript">
Var lBrowser = {};
LBrowser. agt = navigator. userAgent. toLowerCase ();
LBrowser. isW3C = document. getElementById? True: false;
LBrowser. isIE = (lBrowser. agt. indexOf ("msie ")! =-1) & (lBrowser. agt. indexOf ("opera") =-1) & (lBrowser. agt. indexOf ("omniweb") =-1 ));
LBrowser. isNS6 = lBrowser. isW3C & (navigator. appName = "Netscape ");
LBrowser. isOpera = lBrowser. agt. indexOf ("opera ")! =-1;
LBrowser. isGecko = lBrowser. agt. indexOf ("gecko ")! =-1;
LBrowser. ieTrueBody = function (){
Return (document. compatMode & document. compatMode! = "BackCompat ")? Document.doc umentElement: document. body;
};

// Add the innerText attribute to the DOM object in Firefox
If (lBrowser. isNS6) {// firefox innerText define
HTMLElement. prototype. _ defineGetter _ ("innerText ",
Function (){
Return this. textContent;
}
);
HTMLElement. prototype. _ defineSetter _ ("innerText ",
Function (sText ){
This. textContent = sText;
}
);
}

Alert (lBrowser. isNS6 );
</Script>

Appendix DOM Reference:
Firefox DOM reference: http://www.mozilla.org/docs/dom/domref/dom_shortTOC.html
Ie dom reference:
Http://msdn2.microsoft.com/en-us/library/ms533050.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.