AngularJs:自动对焦无法在Firefox中运行

时间:2022-04-16 09:56:15

here is simple directive for autofocus :

这是自动对焦的简单指令:

app.directive('autoFocus', function($timeout) {
    return {
        restrict: 'AC',
        link: function(_scope, _element) {
            $timeout(function(){
                _element[0].focus();
            }, 0);
        }
    };
});

Demo : http://jsfiddle.net/ounsqcmt/55/

演示:http://jsfiddle.net/ounsqcmt/55/

This directive works great in Chrome but doesn't work in Firefox . version 36.00
Any idea ?

该指令在Chrome中运行良好,但在Firefox中不起作用。版本36.00有什么想法吗?

2 个解决方案

#1


it shows a warning in firefox: "Use of getAttributeNode() is deprecated. Use getAttribute() instead."

它在firefox中显示警告:“不推荐使用getAttributeNode()。请改用getAttribute()。”

It seems that it's an internal problem in Firefox, at least one of the warnings is fixed in https://bugzilla.mozilla.org/show_bug.cgi?id=690120 . The problem was raised with jQuery too about the warning that is displayed in the console.

这似乎是Firefox中的一个内部问题,至少有一个警告是在https://bugzilla.mozilla.org/show_bug.cgi?id=690120中修复的。 jQuery也提出了控制台中显示的警告问题。

http://bugs.jquery.com/ticket/12072

but it shows that bug has been fixed by their end.

但它表明bug已经被他们的结局修复了。

This might also neither a problem with FireFox, nor a fault in jQuery. It might be DOM interface issue, due to breaking changes in DOM level 4 API.

这也可能既不是FireFox的问题,也不是jQuery的错误。由于DOM 4级API的重大变化,它可能是DOM接口问题。

See this stack overflow link for additional information:

有关其他信息,请参阅此堆栈溢出链接:

#2


var app = angular.module("App", []);

app.controller("AppCtrl", function($scope) {
       
})

app.directive('autoFocus', function($timeout) {
    return {
        restrict: 'AC',
        link: function(_scope, _element) {
        		
            $timeout(function(){
                 window.focus()
                _element[0].focus();
            }, 0);
        }
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="App" ng-controller="AppCtrl ">
    <form>
        
        <input type="text" />
        <input name="theInput" auto-focus />
        
    </form>
    
    

		


</div>

#1


it shows a warning in firefox: "Use of getAttributeNode() is deprecated. Use getAttribute() instead."

它在firefox中显示警告:“不推荐使用getAttributeNode()。请改用getAttribute()。”

It seems that it's an internal problem in Firefox, at least one of the warnings is fixed in https://bugzilla.mozilla.org/show_bug.cgi?id=690120 . The problem was raised with jQuery too about the warning that is displayed in the console.

这似乎是Firefox中的一个内部问题,至少有一个警告是在https://bugzilla.mozilla.org/show_bug.cgi?id=690120中修复的。 jQuery也提出了控制台中显示的警告问题。

http://bugs.jquery.com/ticket/12072

but it shows that bug has been fixed by their end.

但它表明bug已经被他们的结局修复了。

This might also neither a problem with FireFox, nor a fault in jQuery. It might be DOM interface issue, due to breaking changes in DOM level 4 API.

这也可能既不是FireFox的问题,也不是jQuery的错误。由于DOM 4级API的重大变化,它可能是DOM接口问题。

See this stack overflow link for additional information:

有关其他信息,请参阅此堆栈溢出链接:

#2


var app = angular.module("App", []);

app.controller("AppCtrl", function($scope) {
       
})

app.directive('autoFocus', function($timeout) {
    return {
        restrict: 'AC',
        link: function(_scope, _element) {
        		
            $timeout(function(){
                 window.focus()
                _element[0].focus();
            }, 0);
        }
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="App" ng-controller="AppCtrl ">
    <form>
        
        <input type="text" />
        <input name="theInput" auto-focus />
        
    </form>
    
    

		


</div>