Internet Explorer and JavaScript event currentTarget

后端 未结 13 2222
执笔经年
执笔经年 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:06

    You could always use a closure and pass in the element the event handler has been attached to. Something along these lines...

    function handlerOnClick (currentTarget) {
        return function () {
            console.log(currentTarget);
        } 
    }
    
    var domElement = document.createElement('DIV');
    domElement.innerHTML = 'Click me!';
    domElement.onclick = handlerOnClick(domElement);
    document.body.appendChild(domElement);
    

提交回复
热议问题