Differences between IE and childNodes in FireFox

Source: Internet
Author: User

In Javascript, I believe everyone has tried to use getElementsByTagName and childNodes to traverse nodes. However, getElementsByTagName is obviously inferior to childNodes for complex DOM structure traversal, because childNodes can better process DOM hierarchies. We recommend that you use childNodes first when you need to perform the traversal !!
But unfortunately, childNodes is slightly different in IE and FireFox:
Copy codeThe Code is as follows:
<Head> <script type = "text/javascript">
Function view (){
Var childs1 = $ ('firstdiv '). childNodes;
Var childs2 = $ ('seconddiv '). childNodes;
Alert ("length of FirstDiv:" + childs1.length + "-- length of SecondDiv:" + childs2.length );
}
Var $ = function (id)
{Return document. getElementById (id );}
</Script>
</Head>
<Html>
<Body onload = "view ();">
<! -- The First traversal object with spaces and carriage return between nodes -->
<Div id = "FirstDiv">
<Div> 1 </div>
<Div> 2 </div>
<Div> 3 </div>
</Div>
<! -- The second traversal object. Except for comments, there is no space between nodes and press enter -->
<Div id = "SecondDiv"> <div> first </div> <div> second </div> <div> third </div>
</Html>


Running with IE and Firefox has two different results: IE has a result of, while Firefox has a result. How can this happen?
In terms of structure, objects 1 and 2 are different in that there are carriage return or spaces between child nodes of object 1, while object 2 is a row written to the end. Everyone should have thought of it. IE uses a complete label as a node. In addition to the above, Firefox also divides the content between the end of a tag ">" and the start Letter "<" of the next tag (except for comments, including any text, spaces, carriage returns, and tabs) is also a node. These nodes also have their own unique attributes and values-nodeName = "# text ".
In practical use, when Firefox traverses sub-nodes, add the following in the for Loop:
If (childNode. nodeName = "# text") continue;
Or nodeType = 1.
In this way, you can skip unnecessary operations to make the program run more efficiently.
Appendix:
Node. ELEMENT_NODE = 1
Node. ATTRIBUTE_NODE = 2
Node. TEXT_NODE = 3
Node. CDATA_SECTION_NODE = 4
Node. ENTITY_REFERENCE_NODE = 5
Node. ENTITY_NODE = 6
Node. PROCESSING_INSTRUCTION_NODE = 7
Node. COMMENT_NODE = 8
Node. DOCUMENT_NODE = 9
Node. DOCUMENT_TYPE_NODE = 10
Node. DOCUMENT_FRAGMENT_NODE = 11
Node. NOTATION_NODE = 12

Var node = document.doc umentElement. firstChild;
If (node. nodeType! = Node. COMMENT_NODE)
Alert ("You shoshould comment your code well! ");

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.