简体   繁体   English

如何在 jQuery 中获取数组中的所有元素兄弟...?

[英]How to get all element siblings in an array in jQuery…?

what i would like to do is get all element siblings in a variable in Jquery to have easy access of all its sibling's properties.我想要做的是在 Jquery 的一个变量中获取所有元素兄弟姐妹,以便轻松访问其所有兄弟姐妹的属性。 As you can see the structure, the siblings don't have ids or classes or even any attributes on them apart from the tag name, but how would i get the element with id=>the_only_id_element to return all its siblings without using $('#the_only_id_element').next().next() .....?正如您所看到的结构,兄弟姐妹没有 id 或类,甚至除了标签名称之外没有任何属性,但是我如何获得带有 id=>the_only_id_element 的元素以返回其所有兄弟姐妹而不使用$(' #the_only_id_element').next().next() .....?

<div id="the_only_id_element"> element one </div>
<div> element two </div>
<div> element three </div>
<div> element four </div>

You should use .siblings() :你应该使用.siblings()

$('#the_only_id_element').siblings()

Depends on your expectations you can pass as an param the selector.取决于您的期望,您可以将选择器作为参数传递。

Demo:演示:

 console.log($("#the_only_id_element").siblings());
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="the_only_id_element"> element one </div> <div> element two </div> <div> element three </div> <div> element four </div>

 //This one is tested! var element = $('#the_only_id_element'), required_html = $(element).nextUntil(element, 0); //returns array / siblings after each other in array... //now use array index selector to select any sibling by its position console.log($(required_html[0]).html())
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="the_only_id_element"> element one </div> <div> element two </div> <div> element three </div> <div> element four </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM