关于js中的事件监听大家用的比较多了,无非是判断浏览器是否支持addEventListener和attachEvent,网上搜索关于事件监听的方法也挺多,但是总有些不是很完善。下面的方法中对于添加事件监听的方法是一样的,只不过在取消事件绑定上面做了点手术,现在可以支持匿名函数的使用,所以在绑定事件的时候不再需要给函数单独命名了。
先看demo:
代码如下 |
复制代码 |
<!DOCTYPE html>
<html>
<head profile="http://www.111cn.net">
<meta charset="UTF-8">
<title>grid</title>
<style>
html,body{width:100%;height:100%;margin:0;padding:0;}
div{padding:0;margin:0;font-size:14px;}
#wrap{height:300px;border:5px dotted #ccc;background:#ececec;color:#aaa;font-size:50px;text-align:center;line-height:300px;}
.btn{width:200px;height:30px;display:inline-block;text-align:center;line-height:30px;border:2px solid #ccc;background:#ececec;margin:10px;text-decoration:none;color:#333;}
.btn:hover{background:#ddd;}
</style>
</head>
<body>
<div id="wrap">
点击此处
</div>
<a onclick="bindFn()" href="javascript:;" class="btn">绑定事件</a>
<a onclick="unbindFn()" href="javascript:;" class="btn">取消绑定事件</a>
<script>
/*绑定事件与取消绑定*/
var handleHash = {};
var bind = (function() {
if (window.addEventListener) {
return function(el, type, fn, capture) {
el.addEventListener(type, function(){
fn();
handleHash[type] = handleHash[type] || [];
handleHash[type].push(arguments.callee);
}, capture);
};
} else if (window.attachEvent) {
return function(el, type, fn, capture) {
el.attachEvent("on" + type, function(){
fn();
handleHash[type] = handleHash[type] || [];
handleHash[type].push(arguments.callee);
});
};
}
})();
var unbind = (function(){
if (window.addEventListener) {
return function(el, type ) {
if(handleHash[type]){
var i = 0, len = handleHash[type].length;
for (i; i<len ; i += 1){
el.removeEventListener(type, handleHash[type][i]);
}
};
};
} else if (window.attachEvent) {
return function(el, type) {
if(handleHash[type]){
var i = 0, len = handleHash[type].length;
for (i; i<len ; i += 1){
el.detachEvent("on" + type, handleHash[type][i]);
}
};
};
}
})();
var obj = document.getElementById('wrap');
function bindFn(){
bind(obj,'click',function(){
alert ('click');
});
bind(obj,'click',function(){
alert ('click2');
});
};
function unbindFn(){
unbind(obj,'click');
};
</script>
</body>
</html>
|
代码分析
主要代码:
代码如下 |
复制代码 |
/*绑定事件与取消绑定*/
var handleHash = {};
var bind = (function() {
if (window.addEventListener) {
return function(el, type, fn, capture) {
el.addEventListener(type, function(){
fn();
handleHash[type] = handleHash[type] || [];
handleHash[type].push(arguments.callee);
}, capture);
};
} else if (window.attachEvent) {
return function(el, type, fn, capture) {
el.attachEvent("on" + type, function(){
fn();
handleHash[type] = handleHash[type] || [];
handleHash[type].push(arguments.callee);
});
};
}
})();
var unbind = (function(){
if (window.addEventListener) {
return function(el, type ) {
if(handleHash[type]){
var i = 0, len = handleHash[type].length;
for (i; i<len ; i += 1){
el.removeEventListener(type, handleHash[type][i]);
}
};
};
} else if (window.attachEvent) {
return function(el, type) {
if(handleHash[type]){
var i = 0, len = handleHash[type].length;
for (i; i<len ; i += 1){
el.detachEvent("on" + type, handleHash[type][i]);
}
};
};
}
})(); |
原理解析:
handleHash做哈希表缓存事件的function,handleHash['事件名称']是一个数组,来添加多个事件监听的方法,unbind哪个事件的时候遍历handleHash['事件名称']的数组,然后移除。
使用:
代码如下 |
复制代码 |
bind(obj,'click',function(){
alert ('click');
});
unbind(obj,'click');
|
|