【问题标题】:event.preventdefault is not working in firefoxevent.preventdefault 在 Firefox 中不起作用
【发布时间】:2016-03-19 13:58:38
【问题描述】:

我正在尝试将输入字段中的部分文本设置为只读,以实现我发现此代码但它在 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}}" />

【问题讨论】:

  • 这是一个长镜头 - 尝试使用 preventDefault 切换警报
  • 不要使用alert来调试...
  • 可能问题不在 preventDefault 上。它可能在event.which。参考这个link
  • @AlonEitan 你的远射非常正确!谢谢。
  • 你期待什么,你得到了什么? “不工作”是说服我不要为问题分配时间的字符集。

标签: javascript jquery angularjs events preventdefault


【解决方案1】:

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

希望能帮到你

【讨论】:

    猜你喜欢
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多