如何根据img高度更改img宽度

时间:2012-04-10 12:36:14

标签: jquery image resize height width

我需要根据img高度更改img宽度。 我使用了jQuery height属性,但它不起作用。请参阅下面的功能。

$(function imagesSizer(){
    var img = document.getElementsByClassName('.offer_img');
    if  ($('.offer_img').height() < 210) {
         $('.offer_img').css('width','360px')
    }
});

3 个答案:

答案 0 :(得分:5)

你应该试试

//Wait until the DOM is ready
$(function(){
    //get all images and iterate over them
    $('.offer_img').each(function(){;
        //if the height of this img is < 210
        if  ($(this).height() < 210) {
            //set the width to 360
             $(this).width(360);
        }
    });
});

答案 1 :(得分:0)

更改

var img = document.getElementsByClassName('.offer_img');

var img = document.getElementsByClassName('offer_img');

https://developer.mozilla.org/en/DOM/document.getElementsByClassName

答案 2 :(得分:0)

对于单个元素,

$( function(){
    if ( $('.offer_img')[0].height() < 210 )
        $('.offer_img').width(360);
});
相关问题