我有一个很长的base64
输入我得到的字符串,我想将其设置为src
图像的
$('#img').attr('src',base64data)
console.log('height - ' $('#img').height());
console.log('width - ' $('#img').height());
看起来我回来了0
对彼此而言height
and width
。当然如果我等一会儿
然后我会得到合适的高度。
事情是.attr()
没有回调,我如何获得之后的高度和宽度src
属性已设置但未获取0
后退。 (我认为我得到 0 因为 Jquery 中的更改是异步发生的,但我不能太确定)
$('#img').one('load', function() {
console.log('height - ' $(this).height());
console.log('width - ' $(this).width());
})
.attr('src',base64data);
/* if this image is already cached load event couldn't be fired:
* then look for "complete" status and trigger load event
*/
if ($('#img').get(0).complete) {
$('#img').trigger('load');
}
得到之前width
and height
你需要等待load
事件。
请注意,我使用了one()
方法,因为如果图像已经缓存,则不应触发load
事件两次
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)