首先贴上测试代码<head>   
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />   
<body><button id="botest" onclick="botest()">绑定</button><p/>
<button id="untest" onclick="untest()">取消绑定</button><p/>
<button id="test">click me</button>
<script language=Javascript>   
var LPEvent = new Object();
LPEvent.addListener=function(o, evType, f, capture){
if(o.addEventListener){
    if(typeof(capture)=="undefined")
        capture = false;
o.addEventListener(evType, f, capture);
}else if (o.attachEvent) 
       o.attachEvent("on" + evType, f);
    else 
       eval("o.on" + evType + "=f;");
}; 
LPEvent.removeListener=function(o, evType, f, capture){
if(o.removeEventListener){ 
    if(typeof(capture)=="undefined")
        capture = false;
o.removeEventListener(evType, f, capture);
}else if(o.detachEvent){ 
o.detachEvent("on" + evType, f);
}else{ 
        eval("o.on" + evType + "=null;");
    }
};
Function.prototype.bindAsEventListener  = function(o) {
    var _m = this;
    return function(evt,r) {return _m.call(o, evt || window.event);}
};
function T(){
this.i=0;
this.test=function()
{
    alert(this.i);
}
this.botest=function()
{   
    LPEvent.addListener(document.getElementById("test"),"click",this.test.bindAsEventListener(this),false);
}
this.untest=function()
{   
    LPEvent.removeListener(document.getElementById("test"),"click",this.test.bindAsEventListener(this),false);
}
 
}
var t= new T();
function botest()
{
   t.botest();
 }
function untest()
{   
   t.untest();
}</script>   
   
</body>   
</html>通过这种方式绑定的事件监听方法 this.test.bindAsEventListener(this),无法取消绑定,即this.untest执行无效。盼高手提供解决办法,怎样才能取消绑定呢?

解决方案 »

  1.   


    <!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=gb2312" />   
    <body><button id="botest" onclick="botest()">绑定</button><p/>
    <button id="untest" onclick="untest()">取消绑定</button><p/>
    <button id="test">click me</button>
    <script language=Javascript>   
    var LPEvent = new Object();
    LPEvent.addListener=function(o, evType, f, capture){
        if(o.addEventListener){
            if(typeof(capture)=="undefined")
                capture = false;
            o.addEventListener(evType, f, capture);
        }else if (o.attachEvent){ 
              o['e'+evType+f] = f;
      o[evType+f] = function(){
       o['e'+evType+f](window.event);
      }
      o.attachEvent('on'+evType,o[evType+f]);
        }else 
              eval("o.on" + evType + "=f;");
    }; 
    LPEvent.removeListener=function(o, evType, f, capture){
        if(o.removeEventListener){ 
            if(typeof(capture)=="undefined")
                capture = false;
            o.removeEventListener(evType, f, capture);
        }else if(o.detachEvent){ 
            o.detachEvent('on'+evType,o[evType+f]);
    o[evType+f] = null;

        }else{ 
               eval("o.on" + evType + "=null;");
           }
    };
    Function.prototype.bindAsEventListener  = function(o) {
        var _m = this;
        return function(evt,r) {return _m.call(o, evt || window.event);}
    };
    function T(){
        this.i=0;
        this.test=function()
        {
            alert(this.i);
        }
        this.botest=function()
        {   
            LPEvent.addListener(document.getElementById("test"),"click",this.test.bindAsEventListener(this),false);
        }
        this.untest=function()
        {   
            LPEvent.removeListener(document.getElementById("test"),"click",this.test.bindAsEventListener(this),false);
        }
     
    }
    var t= new T();
    function botest()
    {
       t.botest();
     }
    function untest()
    {   
       t.untest();
    }</script>   
       
    </body>   
    </html>
      

  2.   

    首先谢谢1楼我这边试了下你改的代码,发现还是有些问题,
    1、多次点击“botest”,即重复绑定多个相同函数,只能取消一个绑定的函数,其他的还是没取消掉
    2、此方法对非IE浏览器无效再盼回复
      

  3.   


    <head>   
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />   
    <body><button id="botest" onclick="botest()">绑定</button><p/>
    <button id="untest" onclick="untest()">取消绑定</button><p/>
    <button id="test">click me</button><script language=Javascript>   
    var LPEvent = new Object();
    LPEvent.addListener=function(o, evType, f, capture){
        if(o.addEventListener){
            if(typeof(capture)=="undefined")
                capture = false;
            o.addEventListener(evType, f, capture);
        }
        else if (o.attachEvent) o.attachEvent("on" + evType, f);
        else eval("o.on" + evType + "=f;");
        if(!o.store) o.store = {}; 
        if(!o.store[evType]){
         o.store[evType]=[];
        }
        o.store[evType].push(f);
    }; 
    LPEvent.removeListener=function(o, evType, f, capture){
        if(!o.store) o.store = {}; 
        if(!o.store[evType]){
         o.store[evType]=[];
        }
    if(o.store[evType].length==0) return;
    var ff = o.store[evType].pop();
        if(o.removeEventListener){
            if(typeof(capture)=="undefined")
                capture = false;
            o.removeEventListener(evType, ff, capture);
        }
        else if(o.detachEvent) o.detachEvent("on" + evType, ff);
        else eval("o.on" + evType + "=null;");
    };Function.prototype.bindAsEventListener  = function(o) {
        var _m = this;
        return function(evt,r){return _m.call(o, evt || window.event);}
    };function T(){
        this.i=0;
        this.test=function()
        {
            alert(this.i);
        }
        this.botest=function()
        {   
            LPEvent.addListener(document.getElementById("test"),"click",this.test.bindAsEventListener(this),false);
        }
        this.untest=function()
        {   
            LPEvent.removeListener(document.getElementById("test"),"click",this.test.bindAsEventListener(this),false);
        }
    }
    var t = new T();
    function botest()
    {
       t.botest();
    }
    function untest()
    {   
       t.untest();
    }</script>   
       
    </body>   
    </html>
      

  4.   

    hookee~`学习了~·FF下的取消事件方法想了半天也想不起来了~·还是hookee厉害~·呵呵~·
      

  5.   

    如果可以移除匿名函数才有意义:
    function botest()
    {
       LPEvent.addListener(document.getElementById("test"),"click",function(){alert(0);},false);
     }
    function untest()
    {   
       LPEvent.removeListener(document.getElementById("test"),"click",function(){alert(0);},false);
    }