简体   繁体   中英

Find all specific element in dom and add class on next element

Im trying to find all img in a page and if the next tag is div.txt add class test on this next element.

I've tried this but I dont know why it isnt working:

for(i = 0; i < $('.page img').length; i++){
    $('.page').find('img:eq('+i+')').next('.txt').addClass('teste');    
}

Some help would be appreciated.

You were missing $. No need to loop, use .each() like this:

$('.page img').each(function(){
    $(this).next('.txt').addClass('teste');    
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM