Get image size with Jquery

自作多情 提交于 2019-12-05 12:47:16

I believe this will do what you want:

$('img').each(function() {
    $(this).attr('height',$(this).height());
    $(this).attr('width',$(this).width());
});

You will probably also want to add a class="masonry" attribute to each img as well and then make your selector $('img.masonry').

You could try:

$('img').each(
    function(){
        var height = $(this).height();
        var width = $(this).width();

        $(this).attr({'height': height, 'width': width});
    })

Assuming that you want the height/width attributes to be set to the img's actual height/width, rather than scaled/modified in some manner.

References:

You could use the jQuery .height() and .width() methods, as these return the computed width and height. You should always include the width and height attributes in your img tags however, as some browsers will not render these images correctly without them.

Sure, use the height method to get the size of the img tags.

http://api.jquery.com/height/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!