开发者

jquery - select the next item of this in each() function

开发者 https://www.devze.com 2023-03-20 13:20 出处:网络
I want to select the next element of this in each() function. check out the code comments $(function(){

I want to select the next element of this in each() function. check out the code comments

$(function(){
    var selects = $('body').find('span');
    selects.each(function(index,el){
        if(index==0){
            //remove the next span of this, which is sp开发者_JAVA技巧an2 
        }

    });
}); 


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


Based on your DOM structure, you can do:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号