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 NotoginsengThe 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