相关疑难解决方法(0)

UIWebView stringByEvaluatingJavaScriptFromString

我坚持要在我的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);
}
Run Code Online (Sandbox Code Playgroud)

但是当调用该方法时,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. …
Run Code Online (Sandbox Code Playgroud)

javascript iphone uiwebview uiwebviewdelegate

10
推荐指数
1
解决办法
3万
查看次数

标签 统计

iphone ×1

javascript ×1

uiwebview ×1

uiwebviewdelegate ×1