1
如何讓<input>
元素在對焦時向上箭頭(keyCode 38)或向下箭頭(keyCode 40)做出反應?我爲這個項目使用了jQuery,但是如果更容易的話,請不要在原始JS中編寫它。使輸入框中的向上/向下箭頭沒有任何作用
謝謝!
如何讓<input>
元素在對焦時向上箭頭(keyCode 38)或向下箭頭(keyCode 40)做出反應?我爲這個項目使用了jQuery,但是如果更容易的話,請不要在原始JS中編寫它。使輸入框中的向上/向下箭頭沒有任何作用
謝謝!
像這樣:
$('.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
});
文檔here
或使用[preventDefault()方法(http://api.jquery.com/event.preventDefault/),而不是返回假 – epascarello
我同意preventDefault更清晰 –