【发布时间】:2012-02-06 07:43:51
【问题描述】:
我有一个源代码示例,它以编程方式设置 HTML 中字段的值。
部分源码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
myWebView.delegate = self;
//---formulate the HTML string---
NSString *str = [[[NSString alloc] initWithString:
@"<html>"
" <body>"
" <h1>The fields below are filled in programmatically</h1>"
" <input id='username' value='UserName' />"
" <input id='password' value='Password' type='password' />"
" </body>"
"</html>"
] autorelease];
//---load the UIWebView with the html string---
[myWebView loadHTMLString:str baseURL:nil];
}
//---wait for the UIWebView to finish loading---
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//---access the 2 fields in the HTML---
[myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('username').value='Wei-Meng Lee';"];
[myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('password').value='TopSecret';"];
}
我想在像 Facebook 这样的实际网页中以编程方式设置字段的值,而不是像上面写的那样自定义 HTML。
我应该如何将“str”替换为实际网页的 html 源代码,以便我可以挑选出“ElementbyId”?
【问题讨论】:
-
您正在寻找从 URL 而不是 HTML 打开网页?
-
我想从一个 URL 打开一个网页并能够使用 stringByEvaulatingJavaScriptFromString 函数。
标签: javascript html ios xcode uiwebview