The example in this article summarizes the method of adding and deleting events in JavaScript. Share it with everyone for your reference. The details are as follows:
JavaScript methods for adding and deleting events, that is, making certain methods effective for a specified number of times, which can be once, twice or more, but after the specified number of executions, delete the method to make it invalid. If you often If you are engaged in JS programming, you will know that this function is used more often.
Let’s take a look at a relatively simple example first:
function $(id) { return document.getElementByIdx_x(id); } var ev = null; var count1 = 0; var count2 = 0; var oncount1 = 0; var oncount2 = 0; var isSetEv1 = false; var isSetEv2 = false; //创建事件的通用函数 var EventUtil = function(){}; var flag = new Flag(); //监控变量值 function Flag() { var tempflag = false; var method = null; this.SetMethod = function(value) { method = value; } this.SetValue = function(value) { tempflag = value; if(tempflag == true && method){eval_r(method)} } this.GetValue = function() { return tempflag; } } EventUtil.addEventHandler = function(obj,EventType,Handler) { //如果是FF if(obj.addEventListener) { obj.addEventListener(EventType,Handler,false); } //如果是IE else if(obj.attachEvent) { obj.attachEvent('on'+EventType,Handler); } else { obj['on'+EventType] = Handler; } } //取消事件传入的参数值要跟绑定时完全一样才可以 EventUtil.removeEventHandler = function(obj,EventType,Handler) { //如果是FF if(obj.removeEventListener) { obj.removeEventListener(EventType,Handler,false); } //如果是IE else if(obj.detachEvent) { obj.detachEvent('on'+EventType,Handler); } else { obj['on'+EventType] = Handler; } } function setEvent1(e) { ev = e;//针对火狐获取event相关属性 flag.SetMethod('addList1()'); flag.SetValue (true); } function setEvent2(e) { ev = e;//针对火狐获取event相关属性 flag.SetMethod('addList2()'); flag.SetValue (true); } function isSetEvent1(state) { isSetEv1 = state;//ie下方法名不能和全局变量名相同 } function isSetEvent2(state) { isSetEv2 = state; } function add1(obj) { oncount1 = oncount1 + 1; if(isSetEv1) { obj.innerHTML = "设置了事件,添加了 <b>" + oncount1 + "</b> 篇文章,左边列表1自动增加!"; } else { obj.innerHTML = "没有设置事件,添加了 <b>" + oncount1 + "</b> 篇文章,左边列表1没有变化!"; } } function add2(obj) { oncount2 = oncount2 + 1; if(isSetEv2) { obj.innerHTML = "设置了事件,添加了 <b>" + oncount2 + "</b> 篇文章,左边列表2自动增加!"; } else { obj.innerHTML = "没有设置事件,添加了 <b>" + oncount2 + "</b> 篇文章,左边列表2没有变化!"; } } function addList1() { count1 = count1 + 1; $("list1").innerHTML = "动态添加了 <b>" + count1 + "</b> 篇文章了!"; } function addList2() { count2 = count2 + 1; $("list2").innerHTML = "动态添加了 <b>" + count2 + "</b> 篇文章了!"; }
Let’s look at a simplified example:
//通用的添加和删除事件的方法(兼容IE和firefox) function AddEventHandler(oTarget, sEventType, fnHandler){ if (oTarget.addEventListener) {//非IE oTarget.addEventListener(sEventType, fnHandler, false); }else if (oTarget.attachEvent) {//IE oTarget.attachEvent('on' + sEventType, fnHandler); }else { oTarget['on' + sEventType] = fnHandler; } } function RemoveEventHandler(oTarget, sEventType, fnHandler){ if (oTarget.removeEventListener) {//非IE oTarget.removeEventListener(sEventType, fnHandler, false); }else if (oTarget.detachEvent) {//IE oTarget.detachEvent('on' + sEventType, fnHandler); }else { oTarget['on' + sEventType] = null; } }
Finally let’s look at a complete example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JavaScript添加、删除事件的方法</title> <script type="text/javascript"> var EventUtil=new Object; EventUtil.addEvent=function(oTarget,sEventType,funName){ if(oTarget.addEventListener){//for DOM; oTarget.addEventListener(sEventType,funName, false); }else if(oTarget.attachEvent){ oTarget.attachEvent("on"+sEventType,funName); }else{ oTarget["on"+sEventType]=funName; } }; EventUtil.removeEvent=function(oTarget,sEventType,funName){ if(oTarget.removeEventListener){//for DOM; oTarget.removeEventListener(sEventType,funName, false); }else if(oTarget.detachEvent){ oTarget.detachEvent("on"+sEventType,funName); }else{ oTarget["on"+sEventType]=null; } }; function removeClick(){ alert("click"); var oDiv=document.getElementById("odiv"); oDiv.style.cursor="auto"; EventUtil.removeEvent(oDiv,"click",removeClick); } function addLoadEvent(func){ var oldonload=window.onload; if(typeof window.onload !="function"){ window.onload=func; }else{ window.onload=function(){ oldonload(); func(); } } } addLoadEvent(addClick); function addClick(){ var oDiv=document.getElementById("odiv"); oDiv.style.cursor="pointer"; EventUtil.addEvent(oDiv,"click",removeClick); } </script> </head> <body> <p>第一次点的时候弹出警告,并移除click事件,再点点击就失效了</p> <div id="odiv" style="background:#003399; height:70px; width:126px; margin:0 auto; color:skyblue; ">第一次警告,第二次没反应!</div> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming design.