angularjs autofocus在mozilla firefox中不起作用

时间:2017-12-20 13:26:09

标签: angularjs autofocus

请参阅jsfiddle链接http://jsfiddle.net/ounsqcmt/55/

我使用angularjs

使用了自动对焦

它在谷歌工作,但没有在Mozilla Firefox中工作

代码为

<div ng-app="App" ng-controller="AppCtrl ">
    <form>

        <input type="text" />
        <input name="theInput" auto-focus />

    </form>


</div>

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

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

})

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

以上代码适用于googlechrome和Internet Explorer ...但不能在mozilla firefox中运行..

请帮帮我..

提前致谢

1 个答案:

答案 0 :(得分:0)

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>