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

use*_*202 0 javascript jquery

我想根据父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>
Run Code Online (Sandbox Code Playgroud)

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

Aru*_*hny 5

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

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')
})
Run Code Online (Sandbox Code Playgroud)

演示:小提琴