我有这样的代码,它在 IE 10 或 Firefox 43 中不起作用:
app.directive('limitChars', function () {
return {
restrict: 'A',
link: function (_scope, _element) {
var allowedChars = /[a-z0-9, ]/;
_element.on("keypress", function (e) {
var key = String.fromCodePoint(e.which).toLowerCase();
if (!allowedChars.test(key) && e.which != 13 && e.which != 8) {
return false;
}
});
}
};
});
在 IE 10 中,出现“对象不支持属性或方法 fomCodePoint
”错误。
在 Firefox 43 中,“tab”键将失效。
在 Chrome 中,一切正常。
谁知道为什么?谢谢。
更新:
formCharCode()
现在似乎可以在 IE 中使用了。但是 Tab 键在 Firefox 中仍然不起作用。
最佳答案
同时使用 e.keyCode
和 e.which
。以下适用于所有浏览器:
app.directive('limitChars', function(){
return {
restrict: 'A',
link: function(_scope,_element) {
var allowedChars = /[a-z0-9, ]/;
_element.on("keypress",function(e){
var keyCode = e.keyCode || e.which;
var key = String.fromCharCode(keyCode).toLowerCase();
if (!allowedChars.test(key) && keyCode != 8 && keyCode != 9 && keyCode != 13 && keyCode != 16) {
return false;
}
});
}
};
});
关于javascript - String.fromCodePoint() 在 IE 中不起作用,Tab 键在 Firefox 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34641233/