只获取整个文档中的下一个实例

only get next instance in the entire document

本文关键字:下一个 实例 文档 获取      更新时间:2023-09-26

我只想将目标锁定在单击元素后文档中元素的第一个实例上。这就是我想到的:

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

但这将只能从.class的同级中获取,这意味着如果没有.anotherclass的实例作为同级,它将不会继续遍历文档。

我想让它简单地获取.anotherclass实例,如果我从点击的.class 开始逐行浏览html,我会很好

我该怎么做

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

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

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

全选,然后向下筛选到当前+1的索引。

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

听起来应该分多个步骤进行

$('.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中中断选择过程。所以我会尝试上面的