HTML DOM firstchild LastChild nextSibling previoussibling Property _ Get property value undefined problem

Source: Internet
Author: User

123<title>html Example </title>4<style type= "Text/css" >5 6</style>78<body>9 Ten<ul id= "Ulid" > One<li id= "Li1" >qqqqq</li> A<li id= "Li2" >wwww</li> -<li id= "Li3" >yyyyyy</li> -<li id= "Li4" >test1111</li> the</ul> -  -<script type= "Text/javascript" > -          +         //get UL's first child node Id=li1 -         //Get UL +         varUL1 = document.getElementById ("Ulid"); A         //first child node at         varLi1 =Ul1.firstchild; - alert (li1.id); -  -         //get the last child node -         varLi4 =Ul1.lastchild; - alert (li4.id); in  -         //get the ID of Li is the previous and next sibling node of Li3 to         varLi3 = document.getElementById ("Li3"); + alert (li3.nextSibling.id); - alert (li3.previousSibling.id); the          *</script> $</body>Panax Notoginseng

The above case in Google Chrome and IE is undefined, and print the Lastchildnode display is object text, because in the Advanced browser, the first and last sub-tags obtained through the aforementioned API is a text label (text node), This is similar to the ChildNodes property, so we do not recommend it when we encounter these situations, and we recommend that you get the child elements through the getElementsByTagName () method.

Workaround:

Do not use HTML DOM firstchild properties, HTML DOM lastchild properties, HTML DOM nextSibling properties, HTML DOM previoussibling properties

Use the HTML DOM getElementsByTagName () method instead

123<title>html Example </title>4<style type= "Text/css" >5 6</style>78<body>9 Ten<ul id= "Ulid" > One<li id= "Li1" >qqqqq</li> A<li id= "Li2" >wwww</li> -<li id= "Li3" >yyyyyy</li> -<li id= "Li4" >test1111</li> the</ul> -  -<script type= "Text/javascript" > -          +         varLi=document.getelementsbytagname ("Li"); -          +          for(vari=0;i<li.length;i++) { A alert (li[i].id); at         } -          -</script> -</body> -

HTML DOM firstchild LastChild nextSibling previoussibling Property _ Get property value undefined problem

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.