【问题标题】:angularjs input text focus is working in desktop not working for iPadangularjs输入文本焦点在桌面上工作不适用于iPad
【发布时间】:2020-05-29 22:26:52
【问题描述】:

我的应用程序中有一个搜索输入框。每当我单击按钮时,搜索输入就会打开。但它不是自动对焦。

请看我的代码,

$scope.goSearch = function () {
			$scope.$broadcast("focusTextInput");
}
      
      
 
<a class="nav-link-img search-icon" ui-sref="search" ng-click="goSearch();"><img src="assets/images/searchIconDefault.svg"/></a>

<input type="search" autofocus class="form-control search-field" placeholder="Search Card" ng-model="search"  focus-on="focusTextInput">

 app.directive("focusOn", function ($timeout) {
    return {
      restrict: "A",
      link: function (scope, element, attrs) {
        scope.$on(attrs.focusOn, function (e) {
          $timeout((function () {
            element[0].focus();
          }), 10);
        });
      }
    };
  });

这是我正在使用的代码。

  • ) 唯一的问题是,光标闪烁焦点在台式机、笔记本电脑中有效,但在 ipad 和 ipad pro 中无效。

你能告诉我该怎么做吗?

【问题讨论】:

    标签: javascript html angularjs


    【解决方案1】:

    请尝试以下示例

    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
      $scope.goSearch = function() {
        document.getElementById("search").focus();
      };
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
    
    
    
    <div ng-app="myApp" ng-controller="myCtrl">
    
    <a class="nav-link-img search-icon" ui-sref="search" ng-click="goSearch();">&#128269;</a>
    
    <input type="search" autofocus id="search" class="form-control search-field" placeholder="Search Card" ng-model="search"  focus-on="focusTextInput">
    
    </div>

    【讨论】:

    • 你在输入框中添加id="search"了吗?
    • 是的,我添加了。但是焦点仍然不仅仅在 ipad 和 ipad pro 中起作用。如果我单击文本框,它正在工作。但我想要页面加载。
    • 看来我们别无选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多