【问题标题】:IE input element focus not working with angular directiveIE输入元素焦点不适用于角度指令
【发布时间】:2015-06-12 10:53:41
【问题描述】:

我有一个用输入元素动态填充的 div,并将焦点设置在第一个输入字段上。重点适用于 Chrome,但不适用于 IE。这是plnkr http://plnkr.co/edit/b6urKKilDVRwuqVQfbWt

焦点实际上是在超时函数内完成的,但焦点似乎仍然没有在输入字段上做任何事情。我正在使用一个角度指令来创建我的表单元素。

directive('ngppParameters', ["$compile","$timeout",function($compile,$timeout) {
  return {
        restrict: 'E',       
        link:function($scope,$element,$attrs)
        {
            $scope.$watch("tree.length", function (value) {       

                if(value.length===0)
                    return;                

                $timeout(function() {
                    var fields = $("ngpp-parameters input").filter(":visible:first");
                    console.log("Setting the focus");
                    if(fields.length>0)
                    {
                       console.log("Setting focus");
                       fields[0].focus();                   

                    }
                },10);              
            });
        }        
    };

更新:

这是指令。

【问题讨论】:

    标签: angularjs internet-explorer html-input


    【解决方案1】:

    终于找到问题了,IE正在处理残疾的父母,而不是让焦点集中在孩子身上。

    我已经从指令 pp-field 中删除了 disabled="disabled" 属性,它按预期工作。除 IE 之外的所有其他浏览器都没有考虑父母禁用状态。

    【讨论】:

    【解决方案2】:

    确保使用ng-disabled="" 而不是disabled 属性以使浏览器忽略它并按预期运行。

    【讨论】:

      【解决方案3】:

      fields[0] 不是你想的那样(我之前也犯过同样的错误)。

      你想要fields.eq(0)

      https://api.jquery.com/eq/

      您也可以直接拨打fields.focus()。如果那里只有一个应该可以工作。

      【讨论】:

      • 我刚刚尝试使用 fields.eq(0).focus() 并没有任何区别。焦点仍然不在 IE 中的输入字段上。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      相关资源
      最近更新 更多