jquery,按类查找下一个元素
我如何按类别找到下一个元素。
我尝试使用 $(obj).next('.class');
但这仅返回 $(obj)
父级中的类。 我需要按类名在整个代码中的任何位置获取下一个元素。 因为我的代码看起来像
<table>
<tr><td><div class="class">First</div></td></tr>
<tr><td><div class="class">Second</div></td></tr>
</table>
这可能吗?
How can i find the next element by class.
i tried with $(obj).next('.class');
but this returns classes only in $(obj)
parent.
I need to take the next element anywhere throughout the code by class name.
Because my code looks like
<table>
<tr><td><div class="class">First</div></td></tr>
<tr><td><div class="class">Second</div></td></tr>
</table>
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在这种情况下,您需要转到
then 使用
.next()
,如下所示:或者,如果中间可能有一些行没有
.class
,您可以使用.nextAll()
,如下所示:In this case you need to go up to the
<tr>
then use.next()
, like this:Or if there may be rows in-between without the
.class
inside, you can use.nextAll()
, like this:要查找具有相同类的下一个元素:
To find the next element with the same class:
如果您查看文档 它说:
Next() 获取匹配元素集中每个元素的 紧随其后的同级元素 。如果提供了选择器,它将检索与选择器匹配的下一个同级。
因此,如果第二个 DIV 位于同一个 TD 中,那么您可以编写:
But since it's not, I don't see a point to use next(). You can instead code:
You cannot use next() in this scenario, if you look at the documentation it says:
Next() Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling that matches the selector.
so if the second DIV was in the same TD then you could code:
But since it's not, I don't see a point to use next(). You can instead code:
给定第一个选择器:SelectorA,您可以找到 SelectorB 的下一个匹配项,如下所示:
鼠标悬停更改边框的示例:
更改边框的一般使用示例:
Given a first selector: SelectorA, you can find the next match of SelectorB as below:
Example with mouseover to change border-with:
General use example to change border-with:
通过标签名称查找下一个元素
To find next element by tag name