How can I get a UIWebView to focus on a form input and bring up the keyboard?(如何让 UIWebView 专注于表单输入并调出键盘?)
问题描述
我创建了一个 UIWebView 用于登录 Facebook 的 Graph API OAuth 接口.这很好用,但我必须点击第一个输入字段才能弹出键盘.理想情况下,一旦表单出现,键盘就会出现.所以我一直在试图弄清楚如何让第一个表单字段从 webViewDidFinishLoad:
UIWebViewDelegate 方法中获得焦点,如下所示:
I've created a UIWebView for logging into Facebook's Graph API OAuth interface. This works great, but I have to tap the first input field in order for the keyboard to pop up. Ideally, the keyboard would appear as soon as the form appears. So I've been trying to figure out how to make the first form field get the focus from the webViewDidFinishLoad:
UIWebViewDelegate method, like so:
- (void)webViewDidFinishLoad:(UIWebView *)wv {
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('email')[0].focus();"];
}
唉,这行不通.奇怪的是,当我在 Firefox JavaScript 控制台中运行该 JavaScript 时,它确实将焦点移到了电子邮件字段.但是,当我在 Mac 的 Safari 中运行它时,它什么也不做,就像在 iOS 上一样.FWIW,alert(document.getElementsByName('email')[0])
确实 表明它选择了一个 HTMLInputElement.那么为什么 focus()
在 iOS 或 Mac OS X 中什么都不做呢?
Alas, this does not work. Curiously, when I run that JavaScript in the Firefox JavaScript console, it does move the focus to the email field. When I run it in Safari for the Mac, however, it does nothing, just like on iOS. FWIW, alert(document.getElementsByName('email')[0])
does show that it selects an HTMLInputElement. So why does focus()
do nothing either in iOS or Mac OS X?
推荐答案
现在这在 iOS 6 中成为可能.
This is now possible with iOS 6.
UIWebView* webView = <#your webView here#>;
[webView setKeyboardDisplayRequiresUserAction:NO];
[webView loadHTMLString:@"<html><head></head><body><form><input id="textField" /></form><script>document.getElementById("textField").focus();</script></body></html>"
baseURL:nil];
这篇关于如何让 UIWebView 专注于表单输入并调出键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何让 UIWebView 专注于表单输入并调出键盘?
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Flexslider 箭头未正确显示 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Fetch API 如何获取响应体? 2022-01-01