html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="js_test.js"></script>
</head>
<body>
<input type="button" value="button1" id="button1" onmouseover="change()"/><br>
<input type="button" value="button2" onclick="addListener()"/><br>
<input type="button" value="button3" onclick="removeListener()"/><br></body>
</html>js:function change(){
alert("sf");
}
function addListener(){
var button1 = document.getElementById("button1");
button1.setAttribute("onmouseover", function(){
alert("over");
});
alert("add");
}function removeListener(){

var button1 = document.getElementById("button1");
button1.removeAttribute("onmouseover");
alert("remove");
}
对setAttribute和removeAttribute这两个方法搞不清楚,点击button2时,有效果,可是点击button3时,感觉完全没有效果,帮帮忙,谢谢!

解决方案 »

  1.   

    addEventListener是添加事件监听器,有移除事件监听器的方法不??
      

  2.   

    setAttribute指将function(){alert("over");}函数存储在onmouseover中;
    removeAttribute 则将onmouseover删除;
    所以点击button2时,有效果,可是点击button3时,没有效果
      

  3.   

    可以捕获一下异常看看什么错
    try {
      button1.removeAttribute("onmouseover");
    } catch(e) {
      alert(e);
    }