如果加载相同的图像,javascript,image onload()不会在webkit中触发 [英] javascript, image onload() doesnt fire in webkit if loading same image
问题描述
我有一个< img>
元素,我正在更改其 src
属性。该元素附加了 onload
处理函数。每次我更改src属性和图像加载时,应该运行处理函数。
I have an <img>
element and I'm changing its src
attribute. The element has an onload
handler function attached. Each time i change the src attribute and the image loads the handler function should run.
在Chrome和Safari中,如果我分配的src与之前的src相同,则不运行处理函数。在分配与之前相同的src之前我已经尝试 imgElement.src =''
, imgElement.src = null
, imgElement.src ='notExistingFile.jpg'
并且都不起作用。
In Chrome and Safari, if I assign the same src as the one before, the handler function is not run. Before assigning that same src as before i've tried imgElement.src=''
, imgElement.src= null
, imgElement.src='notExistingFile.jpg'
and none of it works.
请帮忙。以前有人遇到过这个问题吗?
Please help. Anyone had this problem before?
编辑:在分配与之前相同的src之前,通过执行imgElement.src =''工作:
Edit: it worked by doing imgElement.src = '' before assigning the same src as before:
imgElement.src = '';
imgElement.src = 'image.jpg';
推荐答案
这是已知错误。以下是该链接的解决方法:
This is a known bug. Here's the workaround from that link:
这不是错误。 WebKit的价格仅为b
。您必须在替换之前实例化一个新的
Image()
对象,
如下:
This is not a bug. WebKit is just more strict. You must instantiate a new
Image()
object before the replacement, like this:
var photo = document.getElementById('image_id');
var img = new Image();
img.addEventListener('load', myFunction, false);
img.src = 'http://newimgsource.jpg';
photo.src = img.src;
这篇关于如果加载相同的图像,javascript,image onload()不会在webkit中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!