<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>nextSibling</title> </head> <body> <ul id="u1"> <li id="a">javascript</li> <li id="b">jquery</li> <li id="c">html</li> </ul> <ul id="u2"> <li id="d">css3</li> <li id="e">php</li> <li id="f">java</li> </ul> <script type="text/javascript"> var ul = document.getElementsByTagName('ul')[0].firstChild; var li = get_notext(ul); document.write(li.nodeName + "=" + li.innerHTML + "<br/>"); function get_notext(li) { while (li.nodeType != 1) { x = li.nextSibling; } return x; } function get_nextSibling(li) { var x = li.nextSibling while (x.nodeType != 1) { x = x.nextSibling; } return x; } var nextLi = get_nextSibling(li); if (nextLi != null) { document.write(nextLi.nodeName + "=" + nextLi.innerHTML + "<br/>"); } else { document.write("这已经是最后一个节点") } function get_prevSibling(li) { var x = li.previousSibling; while (x.nodeType != 1) { x = x.previousSibling; } return x; } var u2li = document.getElementById("u2").lastChild; var prevLi = get_prevSibling(u2li); if (prevLi != null) { document.write(prevLi.nodeName + "=" + prevLi.innerHTML + "<br/>"); } else { document.write("这已经是最后一个节点") } </script> </body> </html>
你while里面没有写break,所以就无限次执行了呀