http://jsfiddle.net/cyrilkong/XJUGt/4/
function imgRealSize(img) {
var $img = $(img);
if ($img.prop('naturalWidth') === undefined) {
var $tmpImg = $('<img/>').attr('src', $img.attr('src'));
$img.prop('naturalWidth', $tmpImg[0].width);
$img.prop('naturalHeight', $tmpImg[0].height);
}
return {
'width': $img.prop('naturalWidth'),
'height': $img.prop('naturalHeight')
};
}
$(function() {
var target = $('img.dummy');
var target_native_width;
var target_native_height;
target.each(function(index) {
// console.log(index);
imgRealSize(this[index]);
target_native_width = $(this).prop('naturalWidth');
target_native_height = $(this).prop('naturalHeight');
$(this).parent('div').append('<p>This image actual size is ' + target_native_width + ' x ' + target_native_height + ', and css is ' + $(this).width() + ' x ' + $(this).height() + '.</p>');
});
});
它看起來並不像你必須在你調用這個時加載的任何圖像。你知道如何等待圖像加載來詢問高度和寬度嗎? – 2011-03-04 19:34:44