[英]String.fromCodePoint() won't work in IE and tab key won't work in Firefox
我有這樣的代碼,在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;
}
});
}
};
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.