输入框中的向上/向下箭头不做任何事情

Con*_*nor 1 javascript jquery javascript-events

<input>在聚焦时,如何使元素对按向上箭头(keyCode 38)或向下箭头(keyCode 40)没有反应?我正在为项目使用jQuery,但是如果这更容易的话,对于在原始JS中编写它没有任何疑虑.

谢谢!

rea*_*dow 6

像这样:

$('.yourinputclass').keypress(function(e) {
    if(e.which == 38 or e.which == 40) return false; // or you can use e.preventDefault(); like it was mentioned in the comments
});
Run Code Online (Sandbox Code Playgroud)

文档在这里

  • 或使用[preventDefault()](http://api.jquery.com/event.preventDefault/)而不是返回false (2认同)