欢迎您访问我爱IT技术网,今天小编为你分享的javascript教程:【最佳的addEvent事件绑定是怎样诞生的】,下面是详细的讲解!
最佳的addEvent事件绑定是怎样诞生的
// written by Dean Edwards, 2005
// http://dean.edwards.name/function ;addEvent(element, type, handler) {
// assign each event handler a unique ID
// 为事件处理函数设定一个唯一值
if (!handler.$$guid) handler.$$guid=addEvent.guid++;
// create a hash table of event types for the element
if (!element.events) element.events={};
// create a hash table of event handlers for each element/event pair
var handlers=element.events[type];
if (!handlers) {
handlers=element.events[type]={};
// store the existing event handler (if there is one)
// 如果对象已经注册有事件处理,那么要保留下来,并保存为第一个
if (element["on" + type]) {
handlers[0]=element["on" + type];
}
}
// store the event handler in the hash table
handlers[handler.$$guid]=handler;
// assign a global event handler to do all the work
// 指派一个全局函数做统一的事件处理,同时避免了反复注册
element["on" + type]=handleEvent;
};
// a counter used to create unique IDs
addEvent.guid=1;function removeEvent(element, type, handler) {
// delete the event handler from the hash table
if (element.events && element.events[type]) {
delete element.events[type][handler.$$guid];
}
};function handleEvent(event) {
// grab the event object (IE uses a global event object)
event=event || window.event;
// get a reference to the hash table of event handlers
// 这里的 this 随 handlerEvent function 被触发的source element 变化而变化
var handlers=this.events[event.type];
// execute each event handler
for (var i in handlers) {
//这样写才能保证注册的事件处理函数中的 this 得到正确的引用,直接handlers[i]()是不行的
this.$$handleEvent=handlers[i];
this.$$handleEvent(event);
}
};
关于最佳的addEvent事件绑定是怎样诞生的的用户互动如下:
相关问题:有关javascript中addEvent跨浏览器添加事件绑定的问题
答:这里是为了重新调整顺序,把它放在零位置。。 >>详细
相关问题:js解除事件绑定的问题,参数怎么填解除事件绑定需...
答:// 因为你注册事件时的第二个参数函数是匿名函数调用已存函数 // 我不知道这样写是否有效 你试下document.removeEventListerner("touchmove", function (ev) { _this.fnMove(ev); }, false);只有被addEventListener方法添加的事件才可以使用remo... >>详细
相关问题:JS addEvent和removeEvent求逐句注释
答:function addEvent(element, type, handler) {// 检测handler是否有$$guid属性。如果没有,赋值为addEvent的guid属性,将addEvent的guid属性自增1// 假设handler没有$$guid属性,addEvent的guid为1,则经此判断,// handler会有一个$$guid属性,... >>详细
- 【firefox】firefox浏览器不支持innerText的解决
- 【Extjs】Extjs学习过程中新手容易碰到的低级错误
- 【dom】javascript dom追加内容实现示例-追加内容
- 【checkbox】让checkbox不选中即将选中的checkbox
- 【Array】js中更短的 Array 类型转换-类型转换
- 【append】append和appendTo的区别以及appendChil
- 【ExtJs】ExtJs 表单提交登陆实现代码-表单提交-
- 【addClass】javascript自定义的addClass()方法
- 【Event】jquery下为Event handler传递动态参数的
- 【Ajax】jQuery 学习第六课 实现一个Ajax的TreeVi
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
