我知道在处理输入框中的键盘事件时,可以使用以下代码:
$('input').keyup(function(e){
var code = e.keyCode // and 13 is the keyCode for Enter
});
现在我有一些 div
和 li
元素,但我没有一个 form
元素,而且我的元素都不被视为表单元素,也不能接受 focus 或 tab 等操作。
但是现在我需要在一个 div 元素中处理 keyup
(或 keydown
、keypress
都可以)事件。我尝试过:
$('div#modal').keyup(function(e){
if (e.keyCode == 13)
{
$('#next').click(); // Mimicking mouse click to go to the next level.
}
});
但问题是,它不起作用。 我该怎么办?
tabindex
属性使div
接受焦点。请参见我的答案。 - James Allardice