Internet Explorer and JavaScript event currentTarget

后端 未结 13 2244
执笔经年
执笔经年 2020-11-30 05:36

Is there a way to take the current target of an event with IE 7 or 8?

With other browser (firefox, opera, chrome etc.) we can use event.currentTarget or

13条回答
  •  醉酒成梦
    2020-11-30 06:03

    with this function you can pass the object when adding and get it in the listener. the problem about this is that you have an anonymous function as eventlistener and in actionscript you cannot remove an anonymous listener. dunno bout js.

    addEvent:function(object,type,listener,param)
        {
        if(object.addEventListener)
          object.addEventListener(type, function(e){ listener(object, e, param);}, false );
        else
        if(object.attachEvent)
          object.attachEvent('on'+type, function(e){ e = getEvent(e); listener(object, e, param);});
        },
    
    getEvent:function(e)
            {
        if(!e) e = window.event; // || event
        if(e.srcElement) e.target = e.srcElement;
        return e;
        },
    
    removeEvent:function(object,type,listener)
        {
        if(object.removeEventListener)
        object.removeEventListener(type, listener, false);
        else
        object.detachEvent('on'+type, listener);
        }
    
    var div = document.getElementById('noobsafediv');
    var div2 = document.getElementById('noobsafediv2');
    addEvent(div,'mouseover',mouseover,['astring',111,div2]);
    
    
    function mouseover(object,e,param)
     {
     console.log(object,e,param);
     }
    

    its my framework and i call it jNoob.

提交回复
热议问题