IE6中的event.currentTarget [英] event.currentTarget in IE6

查看:132
本文介绍了IE6中的event.currentTarget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个事件处理程序,我附加到一个元素来捕获冒泡的点击事件。
我需要可靠方式来获取抓住事件的元素,而不是触发它的元素。
此特定元素高于srcElement / target的级别数量是任意的,因此使用parentNode或任何不可行的解决方案。
这特别需要在IE6中工作。
虽然我是jQuery框架的粉丝,但我不会在这里使用jQuery,所以任何jQuery建议都会被认为是没用的。

I've got an event handler that I'm attaching to an element to catch a bubbled click event. I need a reliable way of obtaining the element that caught the event, not the element that triggered it. The amount of levels this specific element is above the srcElement/target is arbitrary, thus using parentNode or whatever is not a viable solution. This specifically needs to work in IE6. While I am a bit of a fan of the jQuery framework, I will not be using jQuery in this, so any jQuery suggestions will be considered useless.

tl;博士:我在IE6中需要一个可靠的 event.currentTarget,没有jQuery。

tl;dr: I need a reliable event.currentTarget in IE6, no jQuery.

任何帮助都会是非常感谢。

Any help would be greatly appreciated.

推荐答案

更新: attachEvent 真的好像在这里造成问题。 来自quirksmode.org

Update: attachEvent really seems to create a problem here. From quirksmode.org:


在以下情况中,这指的是窗口:

In the following cases this refers to the window:

element.onclick = function () {doSomething()}
element.attachEvent('onclick',doSomething)
<element onclick="doSomething()">

注意 attachEvent()的存在。 Microsoft事件注册模型的主要缺点是 attachEvent( )创建对函数的引用,不复制它。因此,有时候无法知道哪个HTML当前处理事件。

Note the presence of attachEvent(). The main drawback of the Microsoft event registration model is that attachEvent() creates a reference to the function and does not copy it. Therefore it is sometimes impossible to know which HTML currently handles the event.

解决这个问题的唯一方法似乎是通过创建一个包装器,类似于:

The only way how you could solve this seems to be by creating a wrapper, something like:

var addListener = (function() {
    if(document.attachEvent) {
        return function(element, event, handler) {
            element.attachEvent('on' + event, function() {
                var event = window.event;
                event.currentTarget = element;
                event.target = event.srcElement;
                handler.call(element, event);
            });
        };
     }
     else {
        return function(element, event, handler) {
            element.addEventListener(event, handler, false);
        };
     }
}());

然后这个会引用元素,和 event.currentTarget 一样,你将 event 作为第一个参数传递。

Then this would refer to the element, as would event.currentTarget and you'd pass event as first argument.

用法:

addListener(element, 'click', function(e) {
    // this refers to element
    // e.currentTarget refers to element
    // e.target refers to the element that triggered the event
}); 






旧答案: (适用于内联传统事件处理程序,以及 addEventListener (但不是 attachEvent ))


Old answer: (which works for inline and traditional event handlers, as well as addEventListener (but not attachEvent))

在事件处理程序中,这个引用事件的元素处理程序附加到。

Inside the event handler, this refers to the element the event handler is attached to.

这篇关于IE6中的event.currentTarget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆