我如何在<img>标记使用jQuery

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

本文关键字:jQuery img      更新时间:2023-09-26

我有以下img标签:

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

试图提取304的高度值:

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

但是它总是给我图像的实际大小。什么好主意吗?

试试这个

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

使用

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

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

如果您使用1.6之前的JQuery,则意味着

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

其他

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

会给你答案

尝试获取图像渲染后的高度:

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

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