如何使用 attachEvent 引用调用者对象(“t​​his") [英] How to reference the caller object ("this") using attachEvent

查看:25
本文介绍了如何使用 attachEvent 引用调用者对象(“t​​his")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 IE 中使用 .attachEvent() 方法,如何使用 this 引用调用者对象(触发事件的元素)>?在普通浏览器中,使用.addEventListener,var this指向元素,而在IE中它指向窗口 对象.

我需要它使用以下代码:

var element =//元素,与如何获取无关element.addAnEvent = 函数(名称,函数){if(element.addEventListener)//适用于普通浏览器...否则 if(element.attachEvent){element.attachEvent("on"+name, funct);//其中func中this"的值应该指向element"}}

我刚刚编写了该代码,它与我的代码完全相同,但如果它适用于它,那么它也适用于我!

解决方案

来自 这篇 quirksmode 文章 关于 attachEvent:

<块引用>

  1. 事件总是冒泡,没有捕获的可能性.
  2. 事件处理函数是引用的,不是复制的,所以this关键字总是指向窗口,完全没用.

这两个弱点的结果是,当事件冒泡时,无法知道当前是哪个 HTML 元素在处理该事件.我在活动订单页面上更全面地解释了这个问题.

由于 Microsoft 事件添加模型仅在 Windows 上的 Explorer 5 及更高版本支持,因此不能用于跨浏览器脚本.但即使对于仅在 Windows 上使用资源管理器的应用程序,也最好不要使用它,因为在复杂的应用程序中冒泡问题可能非常棘手.

我还没有测试过,但可能的解决方法是将处理程序包装在一个匿名函数中,该函数通过 funct.call() 调用您的处理程序.

else if(element.attachEvent){element.attachEvent(on"+name, function(){ funct.call( element ) });}

对于未经测试的解决方案,我深表歉意.我不喜欢这样做,但现在不能轻松访问 IE.

Using the method .attachEvent() in IE, how can I reference the caller object (the element that triggered the event) with this? In normal browsers, using .addEventListener, the var this points to the element, while in IE it points to the window object.

I need it to work with the following code:

var element = //the element, doesn't matter how it is obtained
element.addAnEvent = function(name, funct){
   if(element.addEventListener) // Works in NORMAL browsers...
   else if(element.attachEvent){
     element.attachEvent("on"+name, funct);
     //where the value of "this" in funct should point to "element"
   }
}

I just made that code up, it's not exactly the same as my code, but if it works with it then it works with me!

解决方案

From this quirksmode article with regard to attachEvent:

  1. Events always bubble, no capturing possibility.
  2. The event handling function is referenced, not copied, so the this keyword always refers to the window and is completely useless.

The result of these two weaknesses is that when an event bubbles up it is impossible to know which HTML element currently handles the event. I explain this problem more fully on the Event order page.

Since the Microsoft event adding model is only supported by Explorer 5 and higher on Windows, it cannot be used for cross–browser scripts. But even for Explorer–on–Windows only applications it’s best not to use it, since the bubbling problem can be quite nasty in complex applications.

I haven't tested it, but a possible workaround may be to wrap the handler in an anonymous function that calls your handler via funct.call().

else if(element.attachEvent){
   element.attachEvent("on"+name, function(){ funct.call( element ) });
}

My apologies for the untested solution. I don't like doing that, but can't easily get to IE right now.

这篇关于如何使用 attachEvent 引用调用者对象(“t​​his")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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