Javascript iOS - 手动聚焦输入/文本区域的解决方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30752250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - Workaround for manually focusing on an input/textarea
提问by germainelol
So I've seen a lot of threads about the iOS issue with focusing on an input/textarea element. (See hereand here) It seems that iOS will not let you manually focus on one of these elements, and requires it to be a genuine tap/click to focus on the element.
所以我看到了很多关于 iOS 问题的主题,重点是 input/textarea 元素。(见这里和这里)似乎 iOS 不会让你手动关注这些元素之一,并且需要它是真正的点击/点击才能关注元素。
I've tried simulating a click, triggering a click, simply doing click() straight away...all sorts of things.
我试过模拟点击,触发点击,直接点击()......各种各样的事情。
Here is my current workaround that I am trying to implement:
这是我目前正在尝试实施的解决方法:
$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');
}
You don't need to worry about the scrolling function, I know this works and I've tested that enough. The commented out $(e).val('touchstart')works with no issues to change the text of the textarea, but the .focus()does not work on iOS. I've tested this on an Android device and it works fine, but on iOS it just doesn't bring up the keyboard. Sometimes it will start to bring up the keyboard for half a second and then disappear again.
您无需担心滚动功能,我知道这有效并且我已经对其进行了足够的测试。注释掉的$(e).val('touchstart')更改文本区域的文本没有问题,但.focus()在 iOS 上不起作用。我已经在 Android 设备上测试过这个,它工作正常,但在 iOS 上它只是不显示键盘。有时它会开始调出键盘半秒钟,然后再次消失。
I've looked at other threads as I mentioned above, and I can't seem to figure out just how to write a workaround for this.
我已经查看了上面提到的其他线程,但我似乎无法弄清楚如何为此编写解决方法。
Any ideas?
有任何想法吗?
回答by Yogesh Jagdale
I have also harassed same this like issue.
my issue solved by simple one css property that is -webkit-user-select:noneI removed this and all works fine.
try out this.
我也骚扰过同样的问题。我的问题通过简单的一个 css 属性解决了,-webkit-user-select:none我删除了它并且一切正常。试试这个。
回答by Peyman
On your UIWebView, set keyboardDisplayRequiresUserActionto NO.
在您的 上UIWebView,设置keyboardDisplayRequiresUserAction为NO。
回答by Suman Barick
I used iPad Air [iOS 7.0.4], iPad Mini [iOS 7.1] and jQuery [1.11.3] for my test.
我使用 iPad Air [iOS 7.0.4]、iPad Mini [iOS 7.1] 和 jQuery [1.11.3] 进行测试。
The .focus event worked perfectly for me both in input and textarea fields.
.focus 事件在 input 和 textarea 字段中对我来说都非常有效。
I am pasting the code below with which I tested. Please let me know if I am missing anything.
我正在粘贴下面我测试的代码。如果我遗漏了什么,请告诉我。
Is it for some previous version of iOS / jQuery ?
它适用于某些以前版本的 iOS / jQuery 吗?
My HTML,
我的 HTML,
<body>
<!--<input id="in" type="text"/>-->
<textarea id="in"></textarea>
<button id="btn">Add Focus</button>
</body>
My query,
我的查询,
$('#btn').click(function(){
$('#in').focus();
})
回答by bendytree
WKWebView version of keyboardDisplayRequiresUserAction = NOfrom thedyrt/cordova-plugin-wkwebview-engine:
keyboardDisplayRequiresUserAction = NO来自thedyrt/cordova-plugin-wkwebview-engine 的WKWebView 版本:
#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);
}
回答by Ivan Sanz-Carasa
This seems to happen on iOS12 only. .Focus works fine in iOS11&13
这似乎只发生在 iOS12 上。.Focus 在 iOS11&13 中运行良好

