我有input field
:
<input type="text" id="myId" ns-validate="" class="someClass" ng-model="Date" ng-
focus="focus($event, 'Parameters')" ng-blur="blur($event)" tabindex="-1">
焦点事件绑定到此方法:
scope.focus = function (e, names) {
scope.dateSet = false;
$(e.currentTarget).blur();
$(e.currentTarget).prop('readonly', true).addClass('focus');
};
如果我注释掉blur()
没有发生错误,但需要在IE上进行只读工作。
可能由此导致blur事件绑定到此方法:
scope.blur = function (e) {
$(e.currentTarget).prop('readonly', false).removeClass('focus');
};
如果是,为什么这在IE中有效但在Chrome中有效?
正如评论中所建议的,我试图创建一个指令:
angular.module('xxx').directive('blurChecker', function () {
return function (scope, element, attributes) {
scope.$watch(attributes.blurChecker, function(){
element[0].blur();
})
};
});
添加到html
blurChecker='true'
没有任何影响。有什么想法吗?
答案 0 :(得分:1)
始终从指令进行DOM操作。这样可以更好地控制您想要操作的DOM。
在指令中使用链接功能。因为您还需要更新范围变量。然后绑定模糊&amp;将事件聚焦到链接功能中的元素。
当你传递&#34;参数&#34;焦点上的字符串。您可以通过提及元素内的属性(例如parameter="Parameters"
<强> HTML 强>
<input type="text" id="myId" ns-validate="" class="someClass" ng-model="Date"
blur-checker parameter="Parameters" tabindex="-1">
<强>指令强>
angular.module('tpmobile').directive('blurChecker', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var parameterName = attrs.parameter; //I don't know what it does here, but you can get parameter name here
element.on('blur', function(event) {
element.prop('readonly', false).removeClass('focus');
});
element.on('focus', function(e) {
scope.dateSet = false;
element.blur();
element.prop('readonly', true).addClass('focus');
})
}
}
});
希望这可以帮到你。感谢。