任何人都知道如何在IE中禁用退格键并使用Javascript删除密钥?这是我的下面的代码,但似乎它不适用于IE但对Mozilla来说很好.
onkeydown="return isNumberKey(event,this)" function isNumberKey(evt, obj) { var charCode = (evt.which) ? evt.which : evt.keyCode if (charCode == 8 || charCode == 46) return false; return true; }
Joel.. 13
此事件处理程序适用于所有主流浏览器.
function onkeyup(e) { var code; if (!e) var e = window.event; // some browsers don't pass e, so get it from the window if (e.keyCode) code = e.keyCode; // some browsers use e.keyCode else if (e.which) code = e.which; // others use e.which if (code == 8 || code == 46) return false; }
您可以将事件附加到此功能,如:
KooiInc.. 5
更新基于@JoeCoder的评论和我的答案的"过时",我修改了它.
document.querySelector([text input element]).onkeydown = checkKey; function checkKey(e) { e = e || event; return !([8, 46].indexOf(e.which || e.keyCode || e.charCode) > -1); }
另见这个jsFiddle
此事件处理程序适用于所有主流浏览器.
function onkeyup(e) { var code; if (!e) var e = window.event; // some browsers don't pass e, so get it from the window if (e.keyCode) code = e.keyCode; // some browsers use e.keyCode else if (e.which) code = e.which; // others use e.which if (code == 8 || code == 46) return false; }
您可以将事件附加到此功能,如:
更新基于@JoeCoder的评论和我的答案的"过时",我修改了它.
document.querySelector([text input element]).onkeydown = checkKey; function checkKey(e) { e = e || event; return !([8, 46].indexOf(e.which || e.keyCode || e.charCode) > -1); }
另见这个jsFiddle