首页前端开发HTMLimg onload事件绑定

img onload事件绑定

时间2024-01-25 11:42:30发布访客分类HTML浏览456
导读:收集整理的这篇文章主要介绍了html5教程-img onload事件绑定,觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 在需要对img进行onlo...
收集整理的这篇文章主要介绍了html5教程-img onload事件绑定,觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 在需要对img进行onload事件绑定的时候,一般大家都会想到用常规的方法进行事件绑定,如下:

 

[htML] 

!DOCTYPE html PubLIC "-//W3C//DTD XHTML 1.0 TransITional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

html XMlns="https://www.w3.org/1999/xhtml">  

head>  

    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  

    title> img onload事件绑定(错误用法)/title>  

    script type='text/javascript'>  

    window.onload = function(){  

        VAR img = document.getElementById('imgId');   

        img.onload = function(){  

            alert(1);  

        } ;  

    } ;  

    /script>  

/head>  

  

body>  

    img src='images/06.jpg' id='imgId'/>  

/body>  

/html>  

 

此时大家会发现alert(1)并没有执行,这是什么原因呢?特别是在ie和ff浏览器下。

而且在用到jquery插件库的时候会发现,alert除了在ie和opera浏览器不弹出来外,其他浏览器均弹出来,这是为什么呢?!

 

主要是window.onload事件是在页面dom元素加载完后执行,也就包括了img图片中src加载完成。那么img.onload 就不会执行了,

 

因为其是监听img的src是否加载完成。

 

那么,如何对img图片进行onload事件绑定呢?具体代码如下:

 

[html]  

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

html xmlns="https://www.w3.org/1999/xhtml">  

head>  

    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  

    title> img onload事件绑定(正确用法)/title>  

    script type='text/javascript'>  

    window.onload = function(){  

        var img = document.getElementById('imgId');   

        var src = img.getAttribute('src');  

        img.setattribute('src','');  

        img.onload = function(){  

            alert(1);  

        } ;  

        img.setAttribute('src',src);  

    } ;  

    /script>  

/head>  

  

body>  

    img src='images/06.jpg' id='imgId'/>  

/body>  

/html>  

 

这种方法,在各浏览器下均执行alert(1)。

也就是在页面dom元素加载完成后,获得img的dom对象,获得其src属性,再将其src设置为‘’空,然后监听img的onload事件,最后再设置img的src属性即可。

在需要对img进行onload事件绑定的时候,一般大家都会想到用常规的方法进行事件绑定,如下:

 

[html] 

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

html xmlns="https://www.w3.org/1999/xhtml">  

head>  

    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  

    title> img onload事件绑定(错误用法)/title>  

    script type='text/javascript'>  

    window.onload = function(){  

        var img = document.getElementById('imgId');   

        img.onload = function(){  

            alert(1);  

        } ;  

    } ;  

    /script>  

/head>  

  

body>  

    img src='images/06.jpg' id='imgId'/>  

/body>  

/html>  

 

此时大家会发现alert(1)并没有执行,这是什么原因呢?特别是在ie和ff浏览器下。

而且在用到jquery插件库的时候会发现,alert除了在ie和Opera浏览器不弹出来外,其他浏览器均弹出来,这是为什么呢?!

 

主要是window.onload事件是在页面dom元素加载完后执行,也就包括了img图片中src加载完成。那么img.onload 就不会执行了,

 

因为其是监听img的src是否加载完成。

 

那么,如何对img图片进行onload事件绑定呢?具体代码如下:

 

[html]  

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

html xmlns="https://www.w3.org/1999/xhtml">  

head>  

    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  

    title> img onload事件绑定(正确用法)/title>  

    script type='text/javascript'>  

    window.onload = function(){  

        var img = document.getElementById('imgId');   

        var src = img.getAttribute('src');  

        img.setAttribute('src','');  

        img.onload = function(){  

            alert(1);  

        } ;  

        img.setAttribute('src',src);  

    } ;  

    /script>  

/head>  

  

body>  

    img src='images/06.jpg' id='imgId'/>  

/body>  

/html>  

 

这种方法,在各浏览器下均执行alert(1)。

也就是在页面dom元素加载完成后,获得img的dom对象,获得其src属性,再将其src设置为‘’空,然后监听img的onload事件,最后再设置img的src属性即可。

觉得可用,就经常来吧! 欢迎评论哦! html5教程,巧夺天工,精雕玉琢。小宝典献丑了!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

divDOMHTMLjQuerypost-format-gallery

若转载请注明出处: img onload事件绑定
本文地址: https://pptw.com/jishu/586476.html
HTML5的22个技巧 [HTML5]使用Box2dWeb模拟飞行箭矢

游客 回复需填写必要信息