我使用keyDown事件通过判断e.keycode来管理输入标签值,
但是当我想使用 keyDown 事件在 body
元素或 img
上执行某些操作时,
我发现它不起作用,例如:
$('body,img').keyDown(function(e){
console.log(e.keyCode);
});
keyDown 事件仅适用于 input
标记吗?
最佳答案
keyDown 事件仅适用于可聚焦元素。这将包括输入字段、文本区域等,但您可能找不到任何支持 img
元素的浏览器。来自 jQuery docs :
The keydown event is sent to an element when the user first presses a key on the keyboard. It can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.
您可能也对此相关问题感兴趣:Which HTML elements can receive focus?
关于javascript keydown 事件仅适用于 html 输入标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10394912/