有没有办法选择兄弟节点? [英] Is there a way to select sibling nodes?

查看:132
本文介绍了有没有办法选择兄弟节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某些性能原因,我试图找到一种方法来仅选择所选节点的兄弟节点。例如,

For some performance reasons, I am trying to find a way to select only sibling nodes of the selected node. For example,

 <div id="outer">
      <div id="inner1"> </div>
      <div id="inner2"> </div>
      <div id="inner3"> </div>
      <div id="inner4"> </div>
 </div>

如果我选择了inner1节点,有没有办法让我访问它的兄弟姐妹, inner2-4 节点?

If I selected inner1 node, is there a way for me to access its siblings, inner2-4 nodes?

推荐答案

嗯...确定...只需访问父母,然后是孩子。

Well... sure... just access the parent and then the children.

 node.parentNode.childNodes[]

或...使用jQuery:

or... using jQuery:

$('#innerId').siblings()

编辑:Cletus一如既往地鼓舞人心。我进一步挖了。这就是jQuery基本上如何获得兄弟姐妹:

function getChildren(n, skipMe){
    var r = [];
    for ( ; n; n = n.nextSibling ) 
       if ( n.nodeType == 1 && n != skipMe)
          r.push( n );        
    return r;
};

function getSiblings(n) {
    return getChildren(n.parentNode.firstChild, n);
}

这篇关于有没有办法选择兄弟节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆