<script type="text/javascript">
//绑定事件的函数:
/**
* @description 事件绑定,兼容各浏览器
* @param target 事件触发对象
* @param type 事件
* @param func 事件处理函数
*/
function addEvents(target, type, func) {
if (target.addEventListener) //非ie 和ie9
target.addEventListener(type, func, false);
else
if (target.attachEvent) //ie6到ie8
target.attachEvent("on" + type, func);
else
target["on" + type] = func; //ie5
};
//删除事件的函数:
/**
* @description 事件移除,兼容各浏览器
* @param target 事件触发对象
* @param type 事件
* @param func 事件处理函数
*/
function removeEvents(target, type, func){
if (target.removeEventListener)
target.removeEventListener(type, func, false);
else
if (target.detachEvent)
target.detachEvent("on" + type, func);
else
target["on" + type] = null;
};
var f1=function(){alert('41');}
var f2=function(){alert('42');}
addEvents(b4,"click",f1); //绑定事件的函数,b4是按钮id
addEvents(b4,"click",f2); //绑定事件的函数,b4是按钮id
removeEvents(b4,"click",f1);//删除事件的函数,b4是按钮id
</script>
按了b4按钮,什么反应都没有,按道理只移除了b4的f1事件函数,为什么没有执行f2函数呢。如果将removeEvents(b4,"click",f1);这句去掉后,按b4按钮f1和f2函数都会运行。javascript移除事件js
//绑定事件的函数:
/**
* @description 事件绑定,兼容各浏览器
* @param target 事件触发对象
* @param type 事件
* @param func 事件处理函数
*/
function addEvents(target, type, func) {
if (target.addEventListener) //非ie 和ie9
target.addEventListener(type, func, false);
else
if (target.attachEvent) //ie6到ie8
target.attachEvent("on" + type, func);
else
target["on" + type] = func; //ie5
};
//删除事件的函数:
/**
* @description 事件移除,兼容各浏览器
* @param target 事件触发对象
* @param type 事件
* @param func 事件处理函数
*/
function removeEvents(target, type, func){
if (target.removeEventListener)
target.removeEventListener(type, func, false);
else
if (target.detachEvent)
target.detachEvent("on" + type, func);
else
target["on" + type] = null;
};
var f1=function(){alert('41');}
var f2=function(){alert('42');}
addEvents(b4,"click",f1); //绑定事件的函数,b4是按钮id
addEvents(b4,"click",f2); //绑定事件的函数,b4是按钮id
removeEvents(b4,"click",f1);//删除事件的函数,b4是按钮id
</script>
按了b4按钮,什么反应都没有,按道理只移除了b4的f1事件函数,为什么没有执行f2函数呢。如果将removeEvents(b4,"click",f1);这句去掉后,按b4按钮f1和f2函数都会运行。javascript移除事件js
解决方案 »
- 自己写的一个幻灯片,喜欢的可以看一下。做的不好的地方可以指点
- 获取页面元素document.getElementById与document.all
- 正则表达式空格怎么写
- javascript弹出失效
- 这样写,取得Frame里面的值有错,帮忙看看
- 100分!!哪位好心人帮帮小弟啊!!!!!!!!!!!!!
- input控件的最大长度有多少?
- 怎么让一个iframe在提交数据后隐藏!
- 怎样在self.close()时不弹出提示对话框,直接关闭窗口
- 请教一个问题,JAVASCRIPT中使用了alert(" ")会发出声音,请问自己能编写出这样的声音吗?
- 如何让shu标,自动点入此text框框内?
- 通过js控制文本栏的属性,我这里的js好像不对,请帮我看看?
我服了我自己啊!!!!
居然会有这么低级错误,TMD