IE9 detachEvent取消不了写在html代码上进行绑定的事件

本文探讨了在IE9浏览器中使用JavaScript进行事件绑定的方法,并特别关注如何在静态与动态绑定事件的情况下实现事件的取消绑定。通过两个示例展示了直接在HTML标签中绑定事件与通过JavaScript动态绑定事件的区别。

<html>
<body>
<button id="myButton" onclick="changeBg();">点击变色</button>
<button onclick="detachFunction();">DetachFunction</button>
<script language="javascript">
    function changeBg(){
        document.bgColor = 'red';
    }
    function detachFunction() {
        document.bgColor = 'white'; 
        document.getElementById("myButton").detachEvent('onclick', changeBg);
    }
</script>
</body>
</html>


IE9对于写在html标签中进行绑定的事件无法用detachEvent取消绑定。

测试发现动态绑定的事件,才能动态取消绑定

<html>
<body>
<button id="myButton">点击变色</button>
<button onclick="attachFunction();">AttachFunction</button>
<button onclick="detachFunction();">DetachFunction</button>
<script language="javascript">
    function changeBg(){
        document.bgColor = 'red';
    }
    function attachFunction(){
	document.getElementById("myButton").attachEvent("onclick", changeBg);
    }
    function detachFunction() {
        document.bgColor = 'white'; 
        document.getElementById("myButton").detachEvent('onclick', changeBg);
    }
</script>
</body>
</html>


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值