如何查找当前所选对象的所有兄弟姐妹 [英] How to find all Siblings of currently selected object

查看:156
本文介绍了如何查找当前所选对象的所有兄弟姐妹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中查找所有nextSiblings和previousSiblings的完美方式是什么。我尝试了几种方法但没有得到准确的解决方案如果选择了任何元素,我需要得到所有下一个兄弟的长度,不包括空格,任何空格或换行符

What is the perfect way to find all nextSiblings and previousSiblings in javascript. I tried few ways but not getting accurate solution. if any element is selected, I need to get length of all next siblings excluding white-space, any spaces or line-breaks

此外我不想使用jquery为此,我特意从java脚本中查找内容

Also I don't want to use jquery for this, i specifically looking something from java script

请建议

推荐答案

我假设这发生在一个事件处理程序中,其中这个是对你想要影响其兄弟姐妹的目标元素的引用。

I'll assume that this takes place inside an event handler where this is a reference to the targeted element whose siblings you want to affect.

如果没有,则需要进行调整。

If not, adjustments will be needed.

var result = [],
    node = this.parentNode.firstChild;

while ( node ) {
    if ( node !== this && node.nodeType === Node.ELEMENT_NODE ) 
      result.push( node );
    node = node.nextElementSibling || node.nextSibling;
}

// result will contain all type 1 siblings of "this"

这篇关于如何查找当前所选对象的所有兄弟姐妹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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