event.preventdefault 在 Firefox 中不工作
event.preventdefault is not working in firefox
我试图将输入文件中的部分文本设置为只读,以实现我找到这段代码但它在 FIrefox 中不起作用,在 chrome 中工作正常。
发现 event.preventDefault 导致了问题。
试过 return false,也没有用。
Angular JS指令代码
app.directive('partialReadonly', function () {
return {
restrict: 'A',
link: function ($scope, $element, attrs) {
$element.on('keypress, keydown', function (event) {
var readOnlyLength = attrs["partialReadonly"].length;
if ((event.which != 37 && (event.which != 39))
&& (($element[0].selectionStart < readOnlyLength)
|| (($element[0].selectionStart == readOnlyLength) && (event.which == 8)))) {
alert('preventdef');
//return false;
event.preventDefault();
}
});
$scope.load = function () {
alert('load called attrs["partialReadonly"] ' + attrs["partialReadonly"]);
$scope.temp = attrs["partialReadonly"];
// $element[0].value = attrs["partialReadonly"];
};
}
};
});
Html代码
<input type="text" ng-init="load()" ng-model="key" partial-readonly="{{number}}" />
由于@epascarello 和@Alon 不使用警报进行调试而是使用控制台日志,因此从脚本中删除警报可能会有所帮助。
希望我能帮到你
我试图将输入文件中的部分文本设置为只读,以实现我找到这段代码但它在 FIrefox 中不起作用,在 chrome 中工作正常。
发现 event.preventDefault 导致了问题。 试过 return false,也没有用。
Angular JS指令代码
app.directive('partialReadonly', function () {
return {
restrict: 'A',
link: function ($scope, $element, attrs) {
$element.on('keypress, keydown', function (event) {
var readOnlyLength = attrs["partialReadonly"].length;
if ((event.which != 37 && (event.which != 39))
&& (($element[0].selectionStart < readOnlyLength)
|| (($element[0].selectionStart == readOnlyLength) && (event.which == 8)))) {
alert('preventdef');
//return false;
event.preventDefault();
}
});
$scope.load = function () {
alert('load called attrs["partialReadonly"] ' + attrs["partialReadonly"]);
$scope.temp = attrs["partialReadonly"];
// $element[0].value = attrs["partialReadonly"];
};
}
};
});
Html代码
<input type="text" ng-init="load()" ng-model="key" partial-readonly="{{number}}" />
由于@epascarello 和@Alon 不使用警报进行调试而是使用控制台日志,因此从脚本中删除警报可能会有所帮助。
希望我能帮到你