将类添加到< div>中的所有图像使用javascript&反之亦然

问题描述:

所以我想使用javascript为特定< div> 中的所有图像添加一个类。

我已经制作了这个代码,为网页中的所有图片添加了一个类:

So I want to add a class to all the images within particular <div> using javascript.
I have made this code that adds a class to all the images in the webpage:

$(function(){
    $('img').addClass('posts');
});

但我想仅为特定的< div> 。

我需要一个不同的版本,在< div>
中添加课程排除图片

怎么可能?

But i want to add it only for a particular <div>.
And I need a different version that excludes the images from adding class within a <div>
How is it possible?

请使用查找,根据最佳做法更好使用jquery的性能。

please, use find, it's better based on best practices of performance using jquery.

$('#your_div')。find('img')。addClass('your-class')