javascript, image onload() doesnt fire in webkit if loading same image

前端 未结 3 1001
遥遥无期
遥遥无期 2020-12-08 22:02

I have an element and I\'m changing its src attribute. The element has an onload handler function attached. Each time i ch

3条回答
  •  隐瞒了意图╮
    2020-12-08 22:47

    To add to Matt Ball's answer, consider img.onload instead of img.addEventListener().

    var photo = document.getElementById('image_id');
    var img = new Image();
    img.onload = myFunction;
    img.src = 'http://newimgsource.jpg';
    photo.src = img.src;
    

    img.onload is easier to read and works better across user agents.

提交回复
热议问题