How to reference the caller object (“this”) using attachEvent

后端 未结 4 1221
温柔的废话
温柔的废话 2020-11-29 08:56

Using the method .attachEvent() in IE, how can I reference the caller object (the element that triggered the event) with this? In

4条回答
  •  囚心锁ツ
    2020-11-29 09:15

    If you're in IE, you can get the "caller" object, as you call it, by accessing window.event.srcElement within the event handler function. This object is normally referred to as the event target or source.

    var funct = function(event) {
        if ( !event ) {
            event = window.event;
        }
    
        var callerElement = event.target || event.srcElement;
    
        // Do stuff
    };
    

    This code is untested, but should set you off in the right direction.

提交回复
热议问题