iOS - 手动聚焦于输入/文本区域的解决方法

问题描述 投票:0回答:5

所以我看到了很多关于 iOS 问题的线程,重点关注输入/文本区域元素。 (参见herehere)iOS似乎不会让你手动聚焦于这些元素之一,并且要求它是真正的点击/单击才能聚焦于该元素。

我尝试过模拟点击、触发点击、直接立即执行 click()...各种各样的事情。

这是我当前正在尝试实施的解决方法:

$scope.gotoElement = function(eID) {
    // call $anchorScroll()
    $scope.smoothScrollTo(eID).then(function() {
        clickElement('#textarea');
    });          
}

function clickElement(e) {
  $(e).on('touchstart', function() {
    //$(e).val('touchstart');
    $(e).focus();
  });

  $(e).trigger('touchstart');
}

您无需担心滚动功能,我知道它有效并且我已经对其进行了足够的测试。注释掉的

$(e).val('touchstart')
可以毫无问题地更改文本区域的文本,但
.focus()
在 iOS 上不起作用。我已经在 Android 设备上对此进行了测试,它工作正常,但在 iOS 上它只是不调出键盘。有时它会开始调出键盘半秒,然后又消失。

我已经查看了上面提到的其他线程,但我似乎无法弄清楚如何为此编写解决方法。

有什么想法吗?

javascript jquery ios
5个回答
18
投票

我也有类似的问题。 我的问题通过删除这个 css 属性解决,即

-webkit-user-select:none
并且一切正常。

试试这个!


12
投票

在您的

UIWebView
上,将
keyboardDisplayRequiresUserAction
设置为
NO


2
投票

我使用 iPad Air [iOS 7.0.4]、iPad Mini [iOS 7.1] 和 jQuery [1.11.3] 进行测试。

.focus 事件在输入和文本区域字段中都对我来说非常有效。

我粘贴了下面我测试过的代码。如果我遗漏了什么,请告诉我。

适用于某些以前版本的 iOS / jQuery 吗?

我的 HTML,

<body>
    <!--<input id="in" type="text"/>-->
    <textarea id="in"></textarea>
    <button id="btn">Add Focus</button>
</body>

我的询问,

$('#btn').click(function(){
    $('#in').focus();
})

0
投票
来自

thedyrt/cordova-plugin-wkwebview-engine

keyboardDisplayRequiresUserAction = NOWKWebView 版本:

#import <objc/runtime.h>

- (void) viewDidLoad {
    ...

    SEL sel = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:");
    Class WKContentView = NSClassFromString(@"WKContentView");
    Method method = class_getInstanceMethod(WKContentView, sel);
    IMP originalImp = method_getImplementation(method);
    IMP imp = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, id arg3) {
        ((void (*)(id, SEL, void*, BOOL, BOOL, id))originalImp)(me, sel, arg0, TRUE, arg2, arg3);
    });
    method_setImplementation(method, imp);
}

0
投票

这似乎仅发生在 iOS12 上。 .焦点在 iOS11&13 中运行良好

© www.soinside.com 2019 - 2024. All rights reserved.