我一直致力于让一些非常基本的 JS 在我的 UIWebView 中运行。在网络视图的委托中,我有:
- (void)webViewDidFinishLoad:(UIWebView *)wView {
NSString *someHTML = [wView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('box')[0]"];
NSString *allHTML = [wView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
NSLog(@"someHTML: %@", someHTML);
NSLog(@"allHTML: %@", allHTML);
}
但是当调用该方法时,someHTML 为零,而 allHTML 包含 web 视图中 HTML 的整个正文。
我已经在 Safari JS 控制台中运行了 JS,它工作得很好(它找到了类“box”的 div,所以我知道它在 HTML 中)
有什么建议么?
更多信息:
FWIW,以下每个结果也为零
NSString *result = [self.webView stringByEvaluatingJavaScriptFromString: @"document"];
NSLog (@"1. result: %@", result); //should log the document object?
result = [self.webView stringByEvaluatingJavaScriptFromString: @"document.body"];
NSLog (@"2. result: %@", result); //should log the document body
result = [self.webView stringByEvaluatingJavaScriptFromString: @"document.body.getElementsByClassName"];
NSLog (@"3. result: %@", result); //should log a function
result = [self.webView stringByEvaluatingJavaScriptFromString: @"document.body.getElementsByClassName('box')[0]"];
NSLog (@"4. result: %@", result); //should log the node
Changing
NSString *someHTML = [wView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('box')[0]"];
to
NSString *someHTML = [wView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('box')[0].innerHTML;"];
(添加了.innerHTML)
使世界变得不同。 。 。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)