如何使用jQuery选择器来影响同一个类的不同兄弟?

nip*_*ese 3 css jquery

我正在尝试使用Length来计算具有相同类名的一系列div中的图像数量,但我不能让jQuery单独查看每个DIV.

console.log($('.channel_titles_art').each( function(){
        console.log($('.channel_titles_art img').length);
      })
);
Run Code Online (Sandbox Code Playgroud)

例如,我想输出:div one:4 images,div two:2 images,div 3:12 images ...

但相反,它使用channel_titles_art类计算所有div的子图像的总数,并重复多次的数字(对于三个div"18个图像"重复三次)

有没有办法分别遍历每个div?

Nea*_*eal 6

你必须使用this:

$('.channel_titles_art').each( function(){
    console.log($('img', this).length);
})
Run Code Online (Sandbox Code Playgroud)

显示你想要的东西:

$('.channel_titles_art').each( function(){
    var amt_of_images = $('img', this).length;
    console.log(this, 'has '+amt_of_images+' images');
})
Run Code Online (Sandbox Code Playgroud)

这里是小提琴:-)