如何为img绑定load事件 [英] How to bind load event for img

查看:114
本文介绍了如何为img绑定load事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 $('img')。load(function(){})可以正常工作。但是,我想要 img dom 那些最初还没有创建的还可以触发事件。我想知道为什么以下不起作用。

I know $('img').load(function(){}) can work. However, I want the img dom those haven't been created at first can also trigger the event. I wonder why the below doesn't work.

$(window.document).on('load', 'img', function(){
    console.log('load', this);
})


推荐答案

jQuery不支持捕获。所以我不得不使用js的addEventListener。

jQuery doesn't support capture. So I had to use js's addEventListener.

document.body.addEventListener(
    'load',
    function(event){
        var tgt = event.target;
        if( tgt.tagName == 'IMG'){
            console.log('load', tgt.src);
        }
    },
    true
)

这篇关于如何为img绑定load事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆