Javascript 用javascript在手机上显示虚拟键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6837543/
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
Show virtual keyboard on mobile phones in javascript
提问by Adam
I'm creating a mobile version of my site. There's a part of the site where a dialog pops up with a text input. Normally I would just use jQuery to bring focus to the text input, but that's not working. Here's what I'm trying:
我正在创建我网站的移动版本。网站的一部分会弹出一个带有文本输入的对话框。通常我只会使用 jQuery 将焦点放在文本输入上,但这不起作用。这是我正在尝试的:
$("#textinput").focus();
$("#textinput").click();
$("#textinput").trigger("tap"); //jQuery Mobile
None of them seem to work. Any ideas? I've been testing it on my DroidX. I'm using jQuery Mobile but I'm open to other libraries if they help.
它们似乎都不起作用。有任何想法吗?我一直在我的 DroidX 上测试它。我正在使用 jQuery Mobile,但如果其他库有帮助,我也愿意接受。
回答by Mark Kahn
You can't, at least not in iOS (iPhone), and I believe Android as well. It's a usability issue that the keyboard should not be allowed to be triggered except by user input (it's just annoying if it's automatic).
你不能,至少在 iOS (iPhone) 中不能,我相信 Android 也是如此。这是一个可用性问题,除了用户输入之外,不应允许键盘被触发(如果它是自动的,那就很烦人了)。
There are a couple of ways I know of to get around this:
我知道有几种方法可以解决这个问题:
prompt()
opens the keyboard- If you trigger the
.focus()
from within a.click()
event (e.g. from opening your dialog), the keyboard shows up
prompt()
打开键盘- 如果您
.focus()
在.click()
事件中触发(例如,从打开对话框中),键盘会显示
回答by Tony Findeisen
$("#textinput").focus();
$("#textinput").focus();
Opening the keyboard by setting the focus to an input element, will only work if the focus is set within a "user context" (e.g. click, mousedown, mouseup).
通过将焦点设置到输入元素来打开键盘,只有在“用户上下文”(例如单击、鼠标按下、鼠标向上)内设置焦点时才有效。
From the "script context" (setTimeout, callback returned from an ajax call) the keyboard won't show up.
从“脚本上下文”(setTimeout,ajax 调用返回的回调)中,键盘不会出现。
回答by Petar Parabucki
you can use:
您可以使用:
$(textFiled).trigger("focus");
you can put this code in some function that will trigger on opening some overlay, or when the document is ready.
您可以将此代码放在某个函数中,该函数将在打开某些叠加层或文档准备好时触发。