event.preventdefault 在 Firefox 中不起作用

event.preventdefault is not working in firefox

本文关键字:不起作用 Firefox preventdefault event      更新时间:2023-09-26

我正在尝试将输入中的部分文本设置为只读,以实现我找到了这段代码,但它在 FIrefox 中不起作用,在 chrome 中工作正常。

发现 event.preventDefault 导致问题。尝试返回假,这也不起作用。

角度 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"];
            };
        }
    };
});

网页代码

 <input type="text"  ng-init="load()" ng-model="key" partial-readonly="{{number}}" />
由于

@epascarello和@Alon不使用警报进行调试,而是使用控制台日志,因此从脚本中删除警报可能会有所帮助。

希望我有帮助