Compatible Ie,firefox JavaScript code to get the text value of a node _javascript tips

Source: Internet
Author: User
We know that the innerHTML attribute with the browser itself can get the value of the string contained in the node, such as the following nodes:
Copy Code code as follows:

<div id= "test" ><strong>i ' m strong</strong></div>

Pass
Copy Code code as follows:

var Obj=document.getelementbyid ("Test"); alert (obj.innerhtml);//The value returned is <strong>i ' m strong</strong>

If I want to get the text value of the node, it does not include the string that the label is supposed to do. The text value here is: I ' m strong
Non-Mozilla browsers: obj.innertext;//other browsers obj.firstChild.nodeValue;
First, provide a common way to resolve compatibility issues:
The complete code is as follows:
Code
<ptml> <pead> <title>title</title> </pead> <body> <div id= "test" ><stron G>i ' m strong</strong></div> <script type= "Text/javascript" > var Obj=document.getelementbyid (" Test "); Compatible browsers Get node text method function text (e) {var t= ""; If the element is passed in, it continues to traverse its child elements//Otherwise assume it is an array e=e.childnodes| | E Iterate through all child nodes for (Var j=0;j<e.length;j++) {//If not element, append its text value/otherwise, recursively iterate through the child nodes of all elements T+=e[j].nodetype!=1?e[j].nodevalue:text (E[j].childnodes); }//Return area with the text returned T; Alert (text (obj)) </script> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Let Firefox support the innertext implementation code
Reprint please indicate from: http://www.cnblogs.com/wbkt2t/

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.