We usually use onclick, onmouseover and a series of browser-specific behavior events when operating DOM.
So custom events, as the name suggests, are to define your own event types and event processing functions. When you need which event type is needed at the right time, you can call which handler
Browser default events supported by 1.js
Events for specific browser behaviors, or system events, js default events, etc. are all fine. Everyone knows what I am referring to. I will call them js default events below.
I believe everyone has used a series of operations such as event binding and event removal of js default events, such as:
//DOM0级事件处理程序 var oDiv = document.getElementById('oDiv'); oDiv.onclick = function(){ alert("你点击了我"); }
or
//DOM2级事件处理程序 var oDiv = document.getElementById('oDiv'); //非ie oDiv.addEventListener("click",function(){ alert("你点击了我"); },false); //ie oDiv.attachEvent("onclick", function(){ alert("你点击了我"); });
I won’t do too much research. After all, we are going to discuss js custom events. Here is a code that I have encapsulated before to handle js default events:
//跨浏览器的事件处理程序 //调用时候直接用domEvent.addEvent( , , );直接调用 //使用时候,先用addEvent添加事件,然后在handleFun里面直接写其他函数方法,如getEvent; //addEventListener和attachEvent---都是dom2级事件处理程序 var domEvent = { //element:dom对象,event:待处理的事件,handleFun:处理函数 //事件名称,不含“on”,比如“click”、“mouseover”、“keydown”等 addEvent:function(element,event,handleFun){ //addEventListener----应用于mozilla if(element.addEventListener){ element.addEventListener(event,handleFun,false); }//attachEvent----应用于IE else if(element.attachEvent){ element.attachEvent("on"+event,handleFun); }//其他的选择dom0级事件处理程序 else{ //element.onclick===element["on"+event]; element["on"+event] = handleFun; } }, //事件名称,含“on”,比如“onclick”、“onmouseover”、“onkeydown”等 removeEvent:function(element,event,handleFun){ //removeEventListener----应用于mozilla if (element.removeEventListener) { element.removeEventListener(event,handleFun,false); }//detachEvent----应用于IE else if (element.detachEvent) { element.detachEvent("on"+event,handleFun); }//其他的选择dom0级事件处理程序 else { element["on"+event] = null; } }, //阻止事件冒泡 stopPropagation:function(event){ if(event.stopPropagation){ event.stopPropagation(); }else{ event.cancelBubble = true;//IE阻止事件冒泡,true代表阻止 } }, //阻止事件默认行为 preventDefault:function(event){ if(event.preventDefault){ event.preventDefault(); }else{ event.returnValue = false;//IE阻止事件冒泡,false代表阻止 } }, //获得事件元素 //event.target--非IE //event.srcElement--IE getElement:function(event){ return event.target || event.srcElement; }, //获得事件 getEvent:function(event){ return event? event : window.event; }, //获得事件类型 getType:function(event){ return event.type; } };
Let’s continue with the next topic, js custom events
2. Object literal encapsulates js custom events
According to the above package, we can conceive like this
var eventTarget = { addEvent: function(){ //添加事件 }, fireEvent: function(){ //触发事件 }, removeEvent: function(){ //移除事件 } };
I believe this is easier for everyone to understand, and then there is another question that you can think of, that is, js default events, js can correspond one to one, you know which one is that one, then what about our custom events, this one one by one The corresponding mapping table can only be established by ourselves, and then I do this
var eventTarget = { //保存映射 handlers:{}, addEvent: function(){ //处理代码 }, fireEvent: function(){ //触发代码 }, removeEvent: function(){ //移出代码 } };
This is how I constructed this mapping relationship
handlers = { "type1":[ "fun1", "fun2", // "..." ], "type2":[ "fun1", "fun2" // "..." ] //"..." }
In this way, each type can have multiple processing functions to facilitate our future expansion
Next is the actual coding aspect, writing specific processing code...
I believe everyone is already very clear about this idea, I will directly attach the code
//直接量处理js自定义事件 var eventTarget = { //保存事件类型,处理函数数组映射 handlers:{}, //注册给定类型的事件处理程序, //type -> 自定义事件类型, handler -> 自定义事件回调函数 addEvent: function(type, handler){ //判断事件处理数组是否有该类型事件 if(eventTarget.handlers[type] == undefined){ eventTarget.handlers[type] = []; } //将处理事件push到事件处理数组里面 eventTarget.handlers[type].push(handler); }, //触发一个事件 //event -> 为一个js对象,属性中至少包含type属性, //因为类型是必须的,其次可以传一些处理函数需要的其他变量参数。(这也是为什么要传js对象的原因) fireEvent: function(event){ //判断是否存在该事件类型 if(eventTarget.handlers[event.type] instanceof Array){ var _handler = eventTarget.handlers[event.type]; //在同一个事件类型下的可能存在多种处理事件,找出本次需要处理的事件 for(var i = 0; i < _handler.length; i++){ //执行触发 _handler[i](event); } } }, //注销事件 //type -> 自定义事件类型, handler -> 自定义事件回调函数 removeEvent: function(type, handler){ if(eventTarget.handlers[type] instanceof Array){ var _handler = eventTarget.handlers[type]; //在同一个事件类型下的可能存在多种处理事件,找出本次需要处理的事件 for(var i = 0; i < _handler.length; i++){ //找出本次需要处理的事件下标 if(_handler[i] == handler){ break; } } //删除处理事件 _handler.splice(i, 1); } } };
This is a method of calling run
eventTarget.addEvent("eat",function(){ console.log(123); //123 }); eventTarget.fireEvent({type: "eat"});
This method has a disadvantage, the processing event cannot be deleted, because we use a mapping table to do it, and it is not recommended to directly store so much data in the mapping table, which is a bit much.
Another method to extract the processing events (recommended)
function b(){ console.log(123); } eventTarget.addEvent("eat",b); eventTarget.fireEvent({ type: "eat" }); //123 eventTarget.removeEvent("eat",b); eventTarget.fireEvent({type: "eat"}); //空
You can also pass more parameters
eventTarget.fireEvent({ type: "eat", food: "banana" }); function b(data){ console.log(data.food); //banana }
Summary: The literal method has a slight disadvantage, that is, if we accidentally assign a certain attribute to null in the handler function, this will cause our eventTarget method to crash. It seems that prototyping should be a good method and a little safer.
3. Object prototype encapsulates js custom events
Since the previous ideas are basically explained clearly, I will directly attach the code here. You can study the pros and cons, and maybe you can find a better way to solve it...
//自定义事件构造函数 function EventTarget(){ //事件处理程序数组集合 this.handlers = {}; } //自定义事件的原型对象 EventTarget.prototype = { //设置原型构造函数链 constructor: EventTarget, //注册给定类型的事件处理程序, //type -> 自定义事件类型, handler -> 自定义事件回调函数 addEvent: function(type, handler){ //判断事件处理数组是否有该类型事件 if(typeof this.handlers[type] == 'undefined'){ this.handlers[type] = []; } //将处理事件push到事件处理数组里面 this.handlers[type].push(handler); }, //触发一个事件 //event -> 为一个js对象,属性中至少包含type属性, //因为类型是必须的,其次可以传一些处理函数需要的其他变量参数。(这也是为什么要传js对象的原因) fireEvent: function(event){ //模拟真实事件的event if(!event.target){ event.target = this; } //判断是否存在该事件类型 if(this.handlers[event.type] instanceof Array){ var handlers = this.handlers[event.type]; //在同一个事件类型下的可能存在多种处理事件,找出本次需要处理的事件 for(var i = 0; i < handlers.length; i++){ //执行触发 handlers[i](event); } } }, //注销事件 //type -> 自定义事件类型, handler -> 自定义事件回调函数 removeEvent: function(type, handler){ //判断是否存在该事件类型 if(this.handlers[type] instanceof Array){ var handlers = this.handlers[type]; //在同一个事件类型下的可能存在多种处理事件 for(var i = 0; i < handlers.length; i++){ //找出本次需要处理的事件下标 if(handlers[i] == handler){ break; } } //从事件处理数组里面删除 handlers.splice(i, 1); } } };
Call method
function b(){ console.log(123); } var target = new EventTarget(); target.addEvent("eat", b); target.fireEvent({ type: "eat" }); //123
The prototype method has the same function as the direct method...
The above is the entire content of this article, I hope it will be helpful to everyone’s study.