是否有jQuery选择器来完成此任务?

Eri*_*ric 13 javascript jquery jquery-traversing

我有这4个HTML代码段:

我怎样才能使用jQuery .b为任何给定.a元素选择下一个元素,而不管嵌套?

我想要这样的东西:

$('.a').each(function() {
    var nearestB = $(this)./*Something epically wonderful here*/;

    //do other stuff here
});
Run Code Online (Sandbox Code Playgroud)

Eri*_*ric 1

好的,这是Padel解决方案的修改版本,其行为略有不同

var lastA = null;

$('.a, .b').each(function() {
    if($(this).hasClass('a'))
    {
        lastA = $(this);
    }
    else if(lastA)
    {
        doStuff(lastA,this); //doStuff(a,b)
        lastA = null;
    }
});
Run Code Online (Sandbox Code Playgroud)