[英]How to simplify this jquery tree traversal?
HTML HTML
...
<a id="delete1" href="http://www.example.com">TEST</a>
<p>First</p>
<p>Second</p>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<div id="hidden-qsd">123</div>
...
JS JS
var id = $('#delete1').nextUntil("div[id^='hidden']").next().last().attr('id');
I'd like to get the id of the closest "div" starting with "hidden" located after the link "#delete1". 我想得到最近的“div”的id,其中“hidden”位于链接“#delete1”之后。
This previous code is working but I think there is a simpler way to do it. 之前的代码正在运行,但我认为有一种更简单的方法。
$('#delete1').nextAll('[id^="hidden"]').attr('id')
nextAll()
is enough nextAll()
就够了
example jsbin: http://jsbin.com/usowej/3/edit 示例jsbin: http ://jsbin.com/usowej/3/edit
Note: if you have more than one element whose id is starting with hidden
just use 注意:如果您有多个id为
hidden
元素,则只使用
$('#delete1').nextAll('[id^="hidden"]:first').attr('id')
to retrieve just the first occurence, see http://jsbin.com/usowej/4/edit 只检索第一次出现,请参阅http://jsbin.com/usowej/4/edit
You can use nextAll(selector)
to get the next siblings after the element: 您可以使用
nextAll(selector)
来获取元素后面的下一个兄弟:
var id = $('#delete1').nextAll("div[id^='hidden']").prop('id');
http://api.jquery.com/nextAll/ http://api.jquery.com/nextAll/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.