我坚持要在我的UIWebView中运行一些非常基本的JS。在Web视图的委托中,我有:
- (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为nil,而allHTML包含webview中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
答案 0 :(得分:20)
更改
NSString *someHTML = [wView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('box')[0]"];
到
NSString *someHTML = [wView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('box')[0].innerHTML;"];
(添加.innerHTML)
让世界变得与众不同。 。 。