在IE中模拟W3C事件捕获模型 [英] Emulate W3C event capturing model in IE
问题描述
是否可以在Internet Explorer中效仿事件捕获?
Is it possible to emulate event capturing in Internet Explorer?
一个例子:
<a>one</a>
<a>two</a>
<a>three3</a>
<script>
var links = document.getElementsByTagName("A");
for (var i=0; i < links.length; i++) {
links[i].onclick = function(){
alert("clicked");
};
}
</script>
我想防止所有这些点击事件触发。我可以通过一个事件观察器来实现:
I want to prevent all these click events from firing. I can do that with a single event observer:
document.addEventListener("click", function(e) {
e.stopPropagation();
e.preventDefault();
}, true);
如何在IE中执行相同操作? IE< 9不支持 addEventListener
。它支持 attachEvent
,但它没有 useCapture
选项。
How can I do the same in IE? IE < 9 does not support addEventListener
. It does support attachEvent
, but it doesn't have useCapture
option.
我发现 setCapture方法,但它没有看起来与W3捕捉模式有关。
I've found setCapture method, but it doesn't look related to the W3 capturing model.
推荐答案
一般来说,你不能因为事件顺序。在IE中,事件将从目标元素开始冒泡而没有捕获阶段,因此您无法预先捕获它们。
Generally you can't because of the event order. In IE the events will start bubbling from the target element without the capturing phase so you can't catch them beforehand.
只能有一件事情可以做,如果您管理所有事件处理程序,则只能强制 。
There's only one thing you can do, and it's only possible if you manage all the event handlers.
- 使用capture参数为
addEvent
编写包装 -
如果需要捕获,请执行以下
- Write a wrapper for
addEvent
with capture parameter If capturing is required do the following
- 注册一个简单的冒泡
- 一直到父链,保存所有元素到
数组
- 向后退
Array
调用每个元素
- Register a simple bubbling event, with a function that
- Goes all the way up the parent chain, saving all the elements to an
Array
- Travels backward this
Array
invoking the original event handler on each of the elements
这篇关于在IE中模拟W3C事件捕获模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!