如何添加包含特定大小图像的父Div类? [英] How To Add Class To Parent Div which contains image of certain size?

查看:72
本文介绍了如何添加包含特定大小图像的父Div类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanted to add a class to the parent div depending on particular size of the image inside the parent div.

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

But this jquery code doesn't works when i add more div element in the parent container

代码看起来像这样

<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>

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

I want a jquery code which can add class to the main parent div depending on the size of image of certain width.

推荐答案

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

Use the image load handler... like

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')
})

演示:小提琴

这篇关于如何添加包含特定大小图像的父Div类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆