快捷导航

大佬帮忙看看为啥不能输出前一个节点z呢?

nextSiblingjavascriptjqueryhtml</ul>css3phpjava</ul>functionget_nextSibling(n){varx=n.nextSibling;while(x&&x.nodeType!=1){x=x.nextSibling;}returnx;}functionget_previousSibling(n){varx=n.previousSibling;while(x&&x.nodeType!=1){x=x.previousSibling;}returnx;}varx=document.getElementsByTagName("li")[3];document.write(x.nodeName);document.write("=");document.write(x.innerHTML);vary=get_nextSibling(x);if(y!=null){document.write("
nextsibling:");document.write(y.nodeName);document.write("=");document.write(y.innerHTML);}else{document.write("
已经是最后一个节点");}varq=document.getElementsByTagName("li")[3];varz=get_previousSibling(q);if(z!=null){document.write("
previousSibling:");document.write(z.nodeName);document.write("=");document.write(z.innerTEXT)}</script>

免责声明:本内容仅代表回答者见解不代表本站观点,请谨慎对待。

版权声明:作者保留权利,不代表本站立场。

回复

使用道具 举报

参与会员2

varq=document.getElementsByTagName("li")[3];取到li#d这个元素,这个元素没有前一个兄弟元素或者说为空白节点,所以你执行varz=get_previousSibling(q);得到的是null,所以不显示
回复

使用道具 举报

我猜你的下标搞错了吧。下标3是取的第4个元素
回复

使用道具 举报

可能感兴趣的问答

发新帖
  • 微信访问
  • 手机APP