JavaScript 中的 window.event 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31544108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:58:14  来源:igfitidea点击:

What is window.event in JavaScript?

javascriptevents

提问by mfaieghi

I just can't understand what window.event()does in JavaScript. It is used without any definition. Same thing for document.event(). I also don't understand the difference between these two. Do they accept any arguments?

我只是无法理解window.event()JavaScript 中的内容。它在没有任何定义的情况下使用。同样的事情document.event()。我也不明白这两者的区别。他们接受任何论据吗?

回答by Canvas

An event is something that is called when something happens, so for example click and keypress are events.

事件是在发生某些事情时调用的东西,因此例如单击和按键是事件。

The reason however for window.event()is for cross browser compatibility. So here is some javascript:

然而,原因window.event()是为了跨浏览器兼容性。所以这里有一些 javascript:

object.onclick = function(e) {
  // e holds all of the properties of the event
  // You can access the event properties like so e.target
}

Internet Explorer however, doesn't deal with JavaScript the way other browsers do. So for Internet Explorer to deal with the same code as above we would write something on the lines of

但是,Internet Explorer 不像其他浏览器那样处理 JavaScript。因此,为了让 Internet Explorer 处理与上述相同的代码,我们将在以下行中编写一些内容

object.onclick = function() {
  alert(window.event.srcElement); // Same as e.target
}

Or you can combine them both together like so:

或者您可以将它们组合在一起,如下所示:

object.onclick = function(e) {
  e = e || window.event; // Use e if it exists or e will be equal to window.event
  var target = e.target || e.srcElement; // We then use the e.target property but if that doesn't exist we use e.srcElement
  alert(target);
}

回答by Suman Barick

HTML 4 added the ability to let events trigger actions in a browser, like starting a JavaScript when a user clicks on an element.

HTML 4 添加了让事件在浏览器中触发操作的功能,例如当用户单击元素时启动 JavaScript。

Hereyou can check a list of events that might be useful

您可以在此处查看可能有用的事件列表

I don't know if window.event is used now a days. It seems to be a very generic one. Rather you can use more specifics window events like onerror, onload, onresize, onstorageetc

我不知道现在是否使用 window.event。它似乎是一个非常通用的。相反,你可以使用更多的细节窗口的事件,如onerroronloadonresizeonstorage

Before using them be sure to check for browser compatibility.

在使用它们之前,请务必检查浏览器兼容性。

Thank you.

谢谢你。

回答by Cris Mooney

The "informal" window.event is not a "method" of "window", and thus is not called with parenthesis and/or arguments. Instead it is a Microsoft IE only informal global browser "property" of Window used to locate the current active event object (only one event can be active at a time since browsers are single threaded). For more on what an event object might offer, see http://www.w3schools.com/jsref/dom_obj_event.asp

“非正式” window.event 不是“window”的“方法”,因此不带括号和/或参数调用。相反,它是用于定位当前活动事件对象的 Window 的 Microsoft IE 唯一非正式全局浏览器“属性”(由于浏览器是单线程的,因此一次只能激活一个事件)。有关事件对象可能提供的更多信息,请参阅http://www.w3schools.com/jsref/dom_obj_event.asp

Window.event is only needed before IE9, since IE9+ and other browsers pass the event object as the first parameter to the event handler that has been registered. A standard browser handler "function onclick(e)" would access information about the current event by using the passed in local parameter "e" (or whatever the handler author named it), while prior to IE9 that handler's parameter "e" would be undefined requiring the handler to access window.event instead.

Window.event 只在 IE9 之前需要,因为 IE9+ 和其他浏览器将事件对象作为第一个参数传递给已注册的事件处理程序。标准浏览器处理程序“function onclick(e)”将通过使用传入的本地参数“e”(或处理程序作者命名的任何名称)访问有关当前事件的信息,而在 IE9 之前,该处理程序的参数“e”将是undefined 要求处理程序改为访问 window.event。

In practice, this means that when handling an event, "old IE" tolerant code examines the "event" passed in and if not found looks for window.event: function handler(e) {var e=e?e:window.event;}

实际上,这意味着在处理事件时,“旧 IE”容忍代码检查传入的“事件”,如果没有找到则查找 window.event: function handler(e) {var e=e?e:window.event ;}

To try and be as helpful as possible, as far as I am aware (and can find), window.event is informally accepted as part of IE but document.event is not and thus would only work through even less than informal IE tolerance (and is even more likely to vary based on IE version, if it ever worked at all).

为了尽可能地提供帮助,据我所知(并且可以找到),window.event 被非正式地接受为 IE 的一部分,但 document.event 不是,因此只能通过甚至比非正式的 IE 容忍度更小的工作(并且更有可能根据 IE 版本而有所不同,如果它曾经有效的话)。

回答by abs

You can handle events using:

您可以使用以下方法处理事件:

htmlElementObj.onclick = function(e) {
 //e->event object
};

However Internet Explorer doesn't pass event object to handler. Instead you can use window.event object which is being updated immediately after the event was fired.

但是 Internet Explorer 不会将事件对象传递给处理程序。相反,您可以使用在事件触发后立即更新的 window.event 对象。

htmlElementObj.onclick = function(e) {
  e = e || window.event;
}