我想根据父 div 内图像的特定大小向父 div 添加一个类。
但是当我在父容器中添加更多 div 元素时,此 jquery 代码不起作用
代码看起来像这样
<div>
<div>
<div>
<img src='http://4.bp.blogspot.com/-Go-gvkEm4Rw/UszzNls6EYI/AAAAAAAAEfQ/qds_K1jXgLE/s1600/doctype-hi-res1-960x305.jpg'/>
</div>
</div>
</div>
我想要一个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')
})
演示:Fiddle
关于javascript - 如何向包含特定尺寸图像的父 Div 添加类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20993344/