87,910
社区成员
发帖
与我相关
我的任务
分享
$def with (html)
<!DOCTYPE html>
<html>
<head>
<title>LWT - blog </title>
</head>
<body>
<div>
$html
</div>
</body>
</html>
function loadContent(){
if(document.getElementById("blog-content")!=null){
//document.getElementById("blog-content").innerHTML="$html"; //这行是正式代码
//document.getElementById("blog-content").innerHTML="<h2>bbb</h2>"; //来至$html ,输出<h2>bbb</h2>
// document.getElementById("blog-content").innerHTML="<p>haha</p>"; //自己给定的html,输出正常 haha
//document.getElementById("blog-content").outerHTML="<p>hello,world\"'中国</p><br><p>haha</p>"; //手动给定,输出正常,但双引号要这转义\"才行
}
上述代码是browser查看源码中看到的,三行语句,我分别试了一下。如果$html中有双引号 或换行,会提示unterminated string literal错误,
我在想是不是要把源html当中的特殊字符repalce成能正确解析的。不过这好麻烦。
var a = document.createElement('div');
a.outerHTML = $html;
document.getElementsByTagName('div')[0].appendChild(a);