1

我试过使用$element.width()or$element.height()并且两者似乎都无助于检索 ajax 加载图像的值。如何使用 jQuery 或常规 JS 获得这些?图像在 CSS 中没有设置。

4

2 回答 2

3

当您加载内容时,在您的 ajax 成功中使用类似这样的东西

var real_image_sizes = {};
$('your loaded images').one('load', function() {
    //get real image size
    real_image_sizes.width = this.width;   // Note: $(this).width() will not
    real_image_sizes.height = this.height; // work for in memory images.
}).each(function() {
    if(this.complete) $(this).load();
});
于 2012-09-13T12:56:13.537 回答
1

只需使用负载处理程序!

var $img = $('<img/>').attr('src','img_url.jpg').load(function(){ 
    console.log('width: ', $img.width(), ' height: ', $img.height()); 
});
$('body').append($img);

这是一个小提琴

于 2012-09-13T12:59:27.393 回答