找到不是立即的下一个元素? [英] Find the next element that is not immediate?
问题描述
我想在类计数器之后找到第一个span元素,其代码如下:
I want to find the first span element after class counter, in code something like this:
<div class="counter"></div>
<p></p>
<span></span>
next()函数似乎只能找到直接的下一个元素,因此如下所示:
It seems like the next() function only finds the immediate next element, so something like this:
$(".counter").next("span")
不起作用.我一直在使用的方法有点冗长,我想知道是否有更短的方法,就是这样:
Won't work. The way I have been using is a bit lengthy and I was wondering if there was a shorter way, it is this:
$(".counter").nextAll("span").eq(0)
我认为jQuery 3中的closest()
方法可以解决问题,但是我使用的是1.2.6-有更好的方法来做到这一点(我只是错误地使用了next()
吗?)
I think the closest()
method in jQuery 3 will do the trick, but I am using 1.2.6 -- is there a better way to do this (am I just using next()
wrong?)
推荐答案
我认为您的方法是最好的方法.如果感觉不好,只需将其变成插件即可:
I think your method is the best way. And if you feel it doesn't look good just turn it into a plugin:
jQuery.fn.firstAfter = function(filter){
return this.nextAll(filter).eq(0);
}
这篇关于找到不是立即的下一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!