为了账号安全,请及时绑定邮箱和手机立即绑定

按钮5和6都没反应

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

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

<head>

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

<title>事件</title>

</head>


<body>

<input type="button" value="按钮1" onclick="alert('111')"/>

<input type="button" value="按钮2" onclick="aaa()"/>

<input type="button" value="按钮3" id="but3"/>

<input type="button" value="按钮4" id="but4"/>

<input type="button" value="按钮5" id="but5"/>

<input type="button" value="按钮6" id="but6"/>


<script type="text/javascript">

//html方法

function aaa(){

alert("222");

}

//DOM0级

var but3=document.getElementById("but3");

but3.onclick=function(){alert("333");};

//DOM2级

var but4=document.getElementById("but4");

but4.addEventListener("click",aaa,false);

//but4.removeEventListener("click",aaa,false);

//IE

var but5=document.getElementById("but5");

but5.attachEvent("onclick",aaa);

//but5.detachEvent("onclick",aaa);

//跨浏览器


var eventUtil={

addevent:function(ele,type,hander){

if(ele.addEventListener){

ele.addEventListener(type,hander,false);

}

else if(ele.attachEvent){

ele.attachEvent("on"+type,hander);

}

else{

ele["on"+type]=hander;

//ele.onclick===ele["onclick"];

}

},

removeevent:function(ele,type,hander){

if(ele.removeEventListener){

ele.removeEventListener(type,hander,false);

}

else if(ele.detachEvent){

ele.detachEvent("on"+type,hander);

}

else{

ele["on"+type]=null;

}

}

};

var but6=document.getElementById("but6");

eventUtil.addevent(but6,"click",aaa);

//but6.attachEvent("onclick",aaa);

</script>


</body>

</html>


正在回答

3 回答

//IE

var but5=document.getElementById("but5");

but5.attachEvent("onclick",aaa);

//but5.detachEvent("onclick",aaa);

//跨浏览器


这段代码需要注释掉。按钮6就能用,在“//跨浏览器”代码中调用按钮5,按钮5就能用。

1 回复 有任何疑惑可以回复我~
#1

nico_0 提问者

非常感谢!
2016-03-22 回复 有任何疑惑可以回复我~
#2

叶微白_ 回复 nico_0 提问者

IE9,支持这个特性了,现在主流浏览器都支持,用户前五谷歌,IE,火狐,欧朋等均支持addEventListener特性,慕课这个视频有点年代,老师用的还是XP系统了。建议在慕课学的同时,在w3cschool看下相关知识,上面更新的非常及时。以后,跨浏览器问题会越来越少。
2016-03-22 回复 有任何疑惑可以回复我~
#3

叶微白_ 回复 叶微白_

IE8以及以前的老版本支持attachEvent。2016年微软放弃IE了,window10开始用 Microsoft Edge,以后,随着windows发展,IE不再更新,维护。就像XP一样淘汰。学IT的,多了解下前沿的技术信息。
2016-03-22 回复 有任何疑惑可以回复我~

而且第5个按钮仅仅是在IE上才支持的,其他浏览器是不支持的。

0 回复 有任何疑惑可以回复我~
#1

nico_0 提问者

我的IE浏览器更新之后可以支持addEventListener,是不是只有老版本的ie不支持现在的支持了?
2016-03-22 回复 有任何疑惑可以回复我~
#2

丫丫小小笑 回复 nico_0 提问者

好像是IE9以前的不支持addEventListener。。。
2016-03-23 回复 有任何疑惑可以回复我~

//DOM2级

var but4=document.getElementById("but4");

but4.addEventListener("click",aaa,false);

//but4.removeEventListener("click",aaa,false);

//IE

var but5=document.getElementById("but5");

but5.attachEvent("onclick",aaa);

//but5.detachEvent("onclick",aaa);

把这些都注释掉。。。因为前面写的这些是会对后面的能力选择造成错误的。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
DOM事件探秘
  • 参与学习       99548    人
  • 解答问题       1197    个

DOM事件?本课程会通过实例来给小伙伴们讲解如何使用这些事件

进入课程

按钮5和6都没反应

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信