如何将类添加到父Div包含特定大小的图像?

我想根据父div中图像的特定大小向父div添加一个类。

但是当我在父容器中添加更多div元素时,这个jquery代码不起作用

代码看起来像这样

我想要一个jquery代码,可以根据特定宽度的图像大小将类添加到主父div。

使用图像加载处理程序……就像

 jQuery(function () { $('img').load(function () { //check the desired size here if (this.width > 48) { $(this).parents(':eq(2)').addClass('special') } }).filter(function () { return this.complete }).trigger('load') }) 

演示: 小提琴

 $(img).parent().addClass('classname'); 

是的我已经实现了它已经类似你jQuery函数只是差异是我使用高度而不是图像的宽度这里是我正在使用的jQuery函数

`

 $(window).load(function() { $('img';).each(function() { var div = $(this); var $par = $(this).closest('div'); if(this.height >= 100) { $par.addClass('';); } else { $par.addClass('landscape'); } }) }); 

`