2012-06-21 38 views
0

我试图在webview中使用此JavaScript打印HTML,但它实际上是打印<p>HELLO</p>。我该如何解决这个问题?在代码中使用JavaScript在webview中设置HTML内容。

NSString* statement = [NSString stringWithFormat:@"document.getElementById('test').innerText = '<p>HELLO</p>'"]; 

[self.myWebView stringByEvaluatingJavaScriptFromString:statement]; 
+0

http://stackoverflow.com/questions/992348/reading-html-content-from-a-uiwebview只是检查它是否有帮助 – hacker

回答

3

我猜你需要使用innerHTML而不是innerText

因为innerText将简单地分配你在那里写的任何东西。如果您的文本包含html,并且您的元素解析了HTML,那么您必须使用innerHTML。

请尝试以下操作,可能会有所帮助。

NSString* statement = [NSString stringWithFormat:@"document.getElementById('test').innerHTML = '<p>HELLO</p>'"];  
[self.myWebView stringByEvaluatingJavaScriptFromString:statement]; 

注意 你的测试元件必须支持innerHTML属性类似div跨度等