UIWebView不能注入HTML

UIWebView can't inject HTML

本文关键字:HTML 注入 不能 UIWebView      更新时间:2023-09-26

我试图把一些HTML在一个UIWebView的div。虽然代码看起来是正确的,但它不能工作。

如此:

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('"testdiv'").innerHTML = '"Test text here'""]];

这并不:

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('"testdiv'").innerHTML = '"%@'"", htmlString]];

在第二种情况下'htmlString'只是一些HTML内的blockquote。

基本上当我用HTML做的时候,它失败了。我的第一个想法是HTML可能是无效的,但我认为它仍然会尝试显示它。

作为操作的结果,我得到的'错误'只是一个空(非nil)字符串。

任何想法?

编辑:

下面是一个不显示的HTML示例:

<div style="padding-bottom: 20px;"></div><div><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content=
"text/html charset=us-ascii" />
<title></title>
</head>
<body style=
"word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"
class="">
TestTESTEST<br class="" />
<div apple-content-edited="true" class="">
<div style=
"color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"
class="">
<div class="" style="font-size: 10px;"><br class="" /></div>
</div>
</div>
</body>
</html>
</div>

htmlString变量就是blockquote标签中的HTML。

编辑2:

如果我把HTML直接放在文档中,然后加载它,它就可以工作了。只有当我试图通过JS插入它时才会出现问题。

临时解决方案:

由于我要插入的HTML文档只是一个模板我将其复制到代码中并构建整个字符串并将其加载到UIWebView中:

NSString *s = @"<html><body><div1></div1><div2>%@</div2></body></html>", contentToInsert.

理想情况下,我仍然想要得到这个工作,如果我可以通过JS插入HTML,我可以使用多个HTML模板,只是根据需要将内容放入其中。所以,如果你能找到一个解决方案,请告诉我。