javascript 事件 e.which?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3050984/
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
javascript event e.which?
提问by minil
What is the functionality of javascript event e.which? Please brief with example.
javascript 事件 e.which 的功能是什么?请举例说明。
采纳答案by nickf
e.whichis not an event, whichis a property of the eventobject, which most people label as ein their event handlers. It contains the key code of the key which was pressed to trigger the event (eg: keydown, keyup).
e.which不是事件,which是event对象的属性,大多数人e在他们的事件处理程序中标记它。它包含按下以触发事件的键的键码(例如:keydown、keyup)。
document.onkeypress = function(myEvent) { // doesn't have to be "e"
console.log(myEvent.which);
};
With that code, the console will print out the code of any key you press on the keyboard.
使用该代码,控制台将打印出您在键盘上按下的任何键的代码。
回答by Tim Down
whichis a property of Eventobjects. It is defined for key-related and mouse-related events in most browsers, but in both cases is not defined in IE (prior to version 9).
which是Event对象的属性。它在大多数浏览器中为按键相关和鼠标相关事件定义,但在这两种情况下都没有在 IE 中定义(版本 9 之前)。
For mouse-related events, whichspecifies the mouse button that was involved. For IE < 9, the equivalent value is found in window.event.button. Just to complicate things, non-IE browsers also support a buttonproperty of mouse events that sometimes reports a different value from which. Also, browsers sometimes have different values for the same button or combination of buttons. If you stick to using whichin all browsers that support it and buttonin IE < 9, the one constant is that a value of 1 always means the left mouse button was involved (though not necessarily alone).
对于鼠标相关事件,which指定涉及的鼠标按钮。对于 IE < 9,等效值在window.event.button. 更复杂的是,非 IE 浏览器还支持button鼠标事件的属性,该属性有时会报告与which. 此外,浏览器有时对同一按钮或按钮组合具有不同的值。如果您坚持which在所有支持它的浏览器中使用它并且button在 IE < 9 中,一个常量是值 1 总是意味着涉及鼠标左键(尽管不一定单独)。
document.onmousedown = function(e) {
e = e || window.event;
var button = (typeof e.which != "undefined") ? e.which : e.button;
if (button == 1) {
alert("Left mouse button down");
}
};
For a full analysis, I recommend Jan Wolter's article on JavaScript mouse events.
对于完整的分析,我推荐Jan Wolter 的关于 JavaScript 鼠标事件的文章。
For key-related events, whichrelates to the key that has been pressed. For keydownand keyupevents, this is relatively simple: it's the key code for the key pressed, and returns the same value as the event's keyCodeproperty. Since all browsers support the keyCodeproperty and IE < 9 does not support which, you should generally use keyCodefor keydownand keyupevents.
对于与键相关的事件,which与已按下的键相关。对于keydown和keyup事件,这相对简单:它是按下的键的键代码,并返回与事件keyCode属性相同的值。由于所有浏览器都支持该keyCode属性而 IE < 9 不支持which,因此您通常应该使用keyCodeforkeydown和keyupevents。
For keypressevents, the situation is more complicated. For printable character keys, whichis the character code for the key pressed and is supported in more browsers than the charCodeproperty. In IE < 9 the equivalent is again the keyCodeproperty. So for detecting the character typed, the following is a cross-browser approach. Be aware that the code below should not be used for non-printable keys such as arrow keys, which you should instead detect in the keydownevent:
对于keypress事件,情况更为复杂。对于可打印的字符键,which是按下的键的字符代码,并且在比charCode属性更多的浏览器中受支持。在 IE < 9 中,等价物又是keyCode属性。因此,为了检测输入的字符,以下是跨浏览器的方法。请注意,下面的代码不应用于不可打印的键,例如箭头键,您应该在keydown事件中检测到这些键:
document.onkeypress = function(e) {
e = e || window.event;
var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
if (charCode) {
alert("Character typed: " + String.fromCharCode(charCode));
}
};
Again, for more details I recommend Jan Wolter's article on JavaScript key events
再次,有关更多详细信息,我推荐Jan Wolter 的关于 JavaScript 关键事件的文章
回答by KevinOrfas
This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.
此功能已从 Web 标准中删除。虽然一些浏览器可能仍然支持它,但它正在被删除。不要在旧项目或新项目中使用它。使用它的页面或 Web 应用程序可能随时中断。
You should use KeyboardEvent.keyinstead, if it's available.
KeyboardEvent.key如果可用,您应该改用它。
回答by Sayanjyoti Das
During an event, e:
在活动期间,e:
e.which
is same as:
与:
e.keyCode
So both functions allow you to obtain the keycode of the key pressed during a keypress, keydown or keyup event
因此,这两个函数都允许您获取在 keypress、keydown 或 keyup 事件期间按下的键的键码
Many people use ||(OR) to make sure their code works in browsers which don't support which property. Look at the code below:
许多人使用||(OR) 来确保他们的代码在不支持哪个属性的浏览器中工作。看看下面的代码:
document.onkeypress = function(e) {
var key = e.which || e.keyCode;
alert(key);
}

