jquery 仅获取整个文档中的下一个示例

eyh26e7m  于 2023-06-22  发布在  jQuery
关注(0)|答案(3)|浏览(81)

我想简单地只针对文档中单击的元素之后的元素的第一个示例。这是我的想法

$('.class').click(function(){
    var x = $(this).nextAll('.anotherclass').first();
    //do something
});

但这将只能从.class的兄弟中抓取,这意味着如果没有.anotherclass的示例作为兄弟,它将不会继续遍历文档。
我希望它只是抓取.anotherclass示例,如果我从单击的.class开始逐行遍历html,我会很好

我怎么能这样做?

toe95027

toe950271#

试试这个:http://jsfiddle.net/rmwNS/

var x = $('.class, .anotherClass');
var y = x.slice( x.index( $(this) ), x.length).filter('.anotherClass').first();

文档中节点的顺序保留在x中,因此在单击的节点之前切掉所有内容为您提供了查找第一次出现.anotherClass的起点

iyfamqjs

iyfamqjs2#

选择全部,然后向下过滤到仅为当前1 + 1的索引的一个。

$(".anotherclass").eq( $(this).index(".anotherclass") + 1)
yks3o0rb

yks3o0rb3#

听起来你应该分多个步骤来做

$('.class').click(function(){
  var x.length = 0; 
  x = $(this).siblings('.anotherclass').first(); //check siblings
  if (x.length < 1)
      //check here in another portion of code
});

我从来没有听说过在jquery中中断选择过程。所以我会尝试类似上面的东西

相关问题