Javascript Internet Explorer 11中未触发Keyup/Keydown/Keypress事件
我混合使用JS键事件和AngularJS来防止用户在输入字段中输入非数字键(通过指令)。虽然以下内容在Firefox和Chrome中都可以使用,但Internet Explorer似乎忽略了这些事件:Javascript Internet Explorer 11中未触发Keyup/Keydown/Keypress事件,javascript,angularjs,internet-explorer,angularjs-directive,angular-ui,Javascript,Angularjs,Internet Explorer,Angularjs Directive,Angular Ui,我混合使用JS键事件和AngularJS来防止用户在输入字段中输入非数字键(通过指令)。虽然以下内容在Firefox和Chrome中都可以使用,但Internet Explorer似乎忽略了这些事件: app.directive("numberField", function () { return { restrict: "E", scope: { }, link: function (scope, element, at
app.directive("numberField", function ()
{
return {
restrict: "E",
scope:
{ },
link: function (scope, element, attrs, ctrl)
{
$(element).on('keydown', function (event)
{
var key = event.which || event.keyCode;
if (!event.shiftKey && !event.altKey && !event.ctrlKey &&
// numbers
key >= 48 && key <= 57 ||
// Numeric keypad
key >= 96 && key <= 105 ||
// Backspace and Tab and Enter
key == 8 || key == 9 || key == 13 ||
// Home and End
key == 35 || key == 36 ||
// left and right arrows
key == 37 || key == 39 ||
// Del and Ins
key == 46 || key == 45 ||
//Period and Decimal Point
key == 110 || key == 190)
return true;
return false;
});
},
templateUrl: "DirectiveTemplate/NumberField"
};
});
app.指令(“numberField”,函数()
{
返回{
限制:“E”,
范围:
{ },
链接:函数(范围、元素、属性、ctrl)
{
$(元素).on('keydown',函数(事件)
{
var key=event.which | | event.keyCode;
如果(!event.shiftKey&&!event.altKey&&!event.ctrlKey&&
//数字
key>=48&&key=96&&key原来这是我的属性命名不好的一个例子。我用的是“disabled”作为要作为参数传递给指令的属性,IE无法区分该指令的标准HTML disable和我的自定义属性。重命名解决了此问题。在IE11中,这对您有用吗?我没有IE11要检查。抱歉,我现在已经更正了。如果您使用ngKeypress,ngK会更好eydown和ngKeyup