【发布时间】:2015-06-01 10:51:39
【问题描述】:
我正在尝试将一些 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。
基本上,当我使用 HTML 时,它会失败。我首先认为 HTML 可能无效,但我认为它仍会尝试显示它。
由于操作而得到的“错误”只是一个空(非零)字符串。
有什么想法吗?
编辑:
下面是一个不显示的 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 变量就是块引用标签中的那个 HTML。
编辑2:
如果我将 HTML 直接放入文档中,然后加载它就可以了。只有当我尝试通过 JS 插入时才会出现问题。
【问题讨论】:
-
我对此只有一个想法:您提供的 Html 无效。所以删除所有换行符并检查未关闭的标签。
-
可能是您的数据在转义字符之间终止?,您可以使用
NSLog进行检查吗? -
也在浏览器的调试控制台中检查您的数据
-
无效的 HTML 会导致什么都不显示吗? @iphonic I NSLog stringByEval 的结果并返回一个空字符串。我在 webViewFailed 委托中也一无所获。
-
您能提供无效的
htmlString示例吗?
标签: javascript html ios objective-c uiwebview