function get_nextSibling(n) { var x=n.nextSibling; while(x.nodeType!=1) { x=x.nextSibling; } return x; } function get_previousSibling(n) { var x=n.previousSibling; while(x.nodeType!=1) { x=x.previousSibling; } return x; } var x=document.getElementsByTagName("li")[0]; document.write(x.nodeName); document.write(" = "); document.write(x.innerHTML); var y=get_nextSibling(x); if(y!=null){ document.write("<br />nextsibling: "); document.write(y.nodeName); document.write(" = "); document.write(y.innerHTML); }else{ document.write("<br>已经是最后一个节点"); } var z=get_previousSibling(x); if(z!=null) { document.write(z.nodeName); document.write(" = "); document.write(z.innerHTML); }
找第一个节点应该是var x=document.getElementsByTagName("li")[4];,这里要改!因为previousSibling是找之前的,所以从结尾开始找找到前面