- dom元素添加事件-直接绑定-动态绑定
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>dom元素添加事件-直接绑定-动态绑定</title>
<style>
*{
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<button id="but" onclick="fun1()" onmouseover="fun2()">按钮</button>
<button id="btn">按钮事件</button>
<script>
function fun1(){
console.log("你点我",this);
}
but.click();
function fun2(){
console.log("你悬停我");
}
console.log(but.attributes);
but.attributes.removeNamedItem("onmouseover");
console.log(but.attributes.getNamedItem("onmouseover"));
var args = document.createAttribute("onmouseenter")
args.nodeValue="fun2()";
but.attributes.setNamedItem(args);
but.setAttribute("onmousemove",'fun3()');
btn.onclick=function(){
console.log("你点我",this);
this.onclick=null;
}
</script>
</body>
</html>
- 事件的闭包
<!DOCTYPE html>
<html>
<head lang="en"