gpt4 book ai didi

jquery - 使用jquery选择不相邻的元素

转载 作者:行者123 更新时间:2023-12-01 03:00:14 24 4
gpt4 key购买 nike

我有一个 html 文件,其结构大致如下:

<div id="text-container">
<h1>Title 1</h1>
<div class="text"></div>
<div class="text"></div>
<div class="text"></div>
<div class="text"></div>
<h1>Title 2</h1>
<div class="text"></div>
<div class="text"></div>
<div class="text"></div>
<h1>Title 3</h1>
...
</div>

文件是动态构建的,因此标题和文本类 div 的数量可能会有所不同。我想要实现的是插入一个元素,以便能够从一个标题跳转到下一个标题,所以我有点在 prev() 或 next() 之后,但元素不必是兄弟元素。

谁能指出我正确的方向吗?

预先感谢您的帮助

最佳答案

如果您想要 .next() 功能超越紧邻的下一个元素,请尝试 .nextUntil + .next

$("#text-container").click(function() {
var $nextTitle = $(this).nextUntil("h1").next();
alert($nextTitle.text());
});

关于jquery - 使用jquery选择不相邻的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11513155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com