简体   繁体   中英

how do I get the width inside the <img> tag using jQuery

I have the following img tag:

   <img id="product-image" src="http://cf.test.com/images/store_logos/thumbnail/43f1ee4d5fe422e9440ab4afe065bbff899b24b0.jpg" width="228" height="304" style="opacity: 1;">

Trying to extract out the 304 height value:

$('#product-image').height();

However it always gives me the actual size of the image. Any ideas?

Try this

$(document).ready(function(){
    alert("Width :"+$('#product-image').width());
});

用这个

$('#product-image').attr("height");

$('#product-image').prop("height");

Try to get the height after the image rendered:

$(document).ready(function() {
    $('#product-image').load(function() {
        alert($(this).height());
        alert($(this).width());
    });
});

If your using JQuery before 1.6 means

$('#product-image').attr("height")

other wise

$('#product-image').prop("height")

Will give you answer

将图像放入div并取div的大小

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM