Internet Explorer 7/8支持普通事件传递,不需要window.event?

发布于 2024-09-29 00:20:57 字数 343 浏览 1 评论 0原文

众所周知,Internet Explorer 不支持像这样的事件处理函数的事件传递:

function clickHandler(e) {
  // e is undefined in IE
  e = e || window.event;
{

令我惊讶的是,今天我发现它确实支持。我忘记在我的一个函数中执行这个“e = e || window.event”技巧,但它在 IE8 中工作!

我用IE开发工具做了一些测试,e对象是完全定义的,即使在IE7模式下也是如此。

我的问题是,由于我不关心 8 之前的 IE 版本,我是否应该完全放弃 window.event 内容?

It is common knowledge that Internet explorer does not support event passing to event handler functions like this one:

function clickHandler(e) {
  // e is undefined in IE
  e = e || window.event;
{

For my surprise today, I found out that actually it does. I forgot to do this "e = e || window.event" trick in one of my functions, but it was working in IE8!

I did some tests with IE developer tools, the e object was fully defined and it was so even in IE7 mode.

My question is, should I drop the window.event stuff entirely since I do not care for IE versions prior to 8?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

背叛残局 2024-10-06 00:20:57

如果您使用 DOM0 属性方式分配事件处理程序,那么您仍然需要 e = e || window.event; 位,如果您尝试访问 e 的属性,您将收到错误:

document.onclick = function(e) {
    e.cancelBubble = true; // Error
};

如果您使用 attachEvent 那么您是对的,事件参数提供给监听器函数:

document.attachEvent("onclick", function(e) {
    e.cancelBubble = true; // No error
});

If you assign an event handler using the DOM0 property way, then you still need the e = e || window.event; bit and you'll get an error if you try and access a property of e:

document.onclick = function(e) {
    e.cancelBubble = true; // Error
};

If you use attachEvent then you're right, the event parameter is provided to the listener function:

document.attachEvent("onclick", function(e) {
    e.cancelBubble = true; // No error
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文