例如第一次点击黑色区域的时候弹出警告,并移除click事件,也就是第二次再点击的时候就没反应了,整合代码如下: 添加删除事件 var EventUtil=new Object; //oTarget:目标;sEventType:事件名称;funName:事件触发的函数名; EventUtil.addEvent=function(oTarget,sEventType,funName){ //document.getElementById("dd").attachEvent 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){ //document.getElementById("dd").attachEvent 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); } //----多个函数绑定到window.onload上------// 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> 第一次点击黑色区域的时候弹出警告,并移除click事件,也就是第二次再点击的时候就没反应了 第一次点我警告你,第二次点我不理你! [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
第一次点击黑色区域的时候弹出警告,并移除click事件,也就是第二次再点击的时候就没反应了