jquery - 在each()函数中选择下一项

时间:2011-07-16 05:07:54

标签: jquery

我想在each()函数中选择this的下一个元素。看看代码评论

$(function(){
    var selects = $('body').find('span');
    selects.each(function(index,el){
        if(index==0){
            //remove the next span of this, which is span2 
        }

    });
}); 


    <body>
        <div>
            <span>1</span>
        </div>
        <span>2</span>
        <div>
            <span>3</span>
        </div>  
    </body>

1 个答案:

答案 0 :(得分:21)

根据您的DOM结构,您可以:

var selects = $('body').find('span');
selects.each(function(index, el) {
    if(index !== selects.length - 1) {

        // select the next span
        alert(selects.eq(index + 1).text());
    }
});

You can try it here.

相关问题