UIWebView stringByEvaluatingJavaScriptFromString [英] UIWebView stringByEvaluatingJavaScriptFromString

查看:145
本文介绍了UIWebView stringByEvaluatingJavaScriptFromString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持要在我的UIWebView中运行一些非常基本的JS。在Web视图的委托中,我有:

I'm stuck on getting some pretty basic JS to run in my UIWebView. In the web view's delegate, I have :

- (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包含整个body webview中的HTML。

but when the method is invoked, someHTML is nil while allHTML contains the entire body of the HTML in the webview.

我在Safari JS控制台中运行JS并且它工作得很好(它找到了一个类'box'的div,所以我知道它在HTML中)

I've run the JS in the Safari JS console and it works just fine (it finds a div of class 'box' so I know that its in the HTML)

有什么建议吗?

更多信息:

FWIW,结果以下各项也是零

FWIW, result in each of the following is also nil

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


推荐答案

更改

NSString *someHTML = [wView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('box')[0]"];   

NSString *someHTML = [wView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('box')[0].innerHTML;"];   

(添加.innerHTML)

(added the .innerHTML)

让世界变得与众不同。 。 。

makes all the difference in the world . . .

这篇关于UIWebView stringByEvaluatingJavaScriptFromString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆