事件绑定与解绑
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#box{
width: 100%;
height: 600px;
background: red;
overflow: hidden;
}
#box1{
width: 80%;
height: 400px;
margin: 100px auto;
background: blue;
overflow: hidden;
}
#box2{
width: 60%;
height: 200px;
margin: 100px auto;
background: yellow;
}
</style>
</head>
<body>
<div id="box">
<div id="box1">
<div id="box2">
</div>
</div>
</div>
<script type="text/javascript">
var box = document.getElementById("box")
var box1 = document.getElementById("box1")
var box2 = document.getElementById("box2")
fn(box,'click',abc)// 给box绑定
fn(box,'click',bcd)
// unfn(box,'click',abc)// 解除box绑定的abc函数
// unfn(box,'click',bcd)// 解除box绑定的bcd函数
function fn( obj, click , fn1 ){
if(obj.attachEvent){
obj.attachEvent('on'+ click,fn1)
}else{
obj.addEventListener(click, fn1,false)
}
}
function unfn(obj, click , fn1 ){
if(obj.detachEvent){
obj.detachEvent('on'+ click,fn1)
}else{
obj.removeEventListener(click,fn1,false)
}
}
function abc(){
alert('yiyiyiyi');
}
function bcd(){
alert('erererer');
}
</script>
</body>
</html>