有没有更好的方法可以用jquery更新图像的高度和宽度

Is there a better way to update the height and width of an image with jquery?

本文关键字:高度 图像 更新 方法 更好 有没有 jquery      更新时间:2023-11-09

动态加载新拇指后,我必须更新图像的高度和宽度。我现在是这样做的:

...
$('#thumb_' + newID).attr('width', 120);
$('#thumb_' + newID).attr('height', 120);
$('#thumb_' + newID).attr('src', data.thumb + '?v=' + ( new Date() ).getTime());

似乎总是在设置高度和宽度,但图像并不总是符合指定的尺寸。

有没有更好更可靠的方法来做到这一点?

试试这个;在更改源后使用css函数而不是attr函数

var newsource=data.thumb + '?v=' + ( new Date() ).getTime();
$('#thumb_' + newID).attr('src', newsource).css({height : 120, width : 120});