我的JS无法在Objective-C中获取div的内容

时间:2013-11-27 21:12:26

标签: javascript iphone objective-c cocoa

我正在设法从Webview中检索一些文本。以下是我的HTML文件的示例。

Template.html:

<html>
<body>
        <div id="content" contenteditable="true" style="font-family: Helvetica">
            hello
            </div>
</body>
</html>

(上面的代码就是我在template.html文件中的全部内容。)

然后我跑:

NSString *js = @"document.getElementById('content').innerHTML";
NSString* html = [panel stringByEvaluatingJavaScriptFromString:js];

它返回NOTHING! Xcode是否停止支持这种类型的JS?

请帮助,非常感谢!

--------- EDIT ------------ 有关详细信息,请按以下方式加载页面:

NSBundle *bundle = [NSBundle mainBundle];
NSURL* temp = [bundle URLForResource:@"template" withExtension:@"html"];
[panel loadRequest:[NSURLRequest requestWithURL:temp]];

2 个答案:

答案 0 :(得分:0)

使用下面的行

加载要加载的UIWebView内容
NSString *html = [webView stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"];

来源:More - details- stackoverflow

由于

答案 1 :(得分:0)

我最后发现的实际上是UIWebViewDelegate的问题!

-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *js = @"getContent();";//This function is used to get 
                                //the innerHTML of the document.
NSString* html = [panel stringByEvaluatingJavaScriptFromString:js];
L22(@"content HTML:",html);
}

这将在我的div中提供文字。

在从中提取任何信息之前,请务必确保网站已满载!:0)