在innerHTML中插入评论 [英] Inserting a comment in innerHTML
本文介绍了在innerHTML中插入评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在innerHTML内插入HTML注释,但在IE8中不接受.FF工作正常.
I am trying to insert a HTML comment inside a innerHTML but it is not accepting in IE8. FF works fine.
<html>
<head>
<title>testing</title>
<script language="javascript">
function testcmt() {
var el2 = document.createElement("div");
el2.innerHTML = "<!--Sample--><p>Sample</p>";
alert( el2.innerHTML );
}
</script>
</head>
<body id="BodyID">
<h2>Test</h2>
<input type="button" value="Sample" onmousedown="testcmt(); return false">
</body>
IE仅显示&p; Sample</p>
,并且缺少注释.有指针吗?
IE shows only <p>Sample</p>
and the comment is missing. Any pointers?
推荐答案
您最有可能应该使用 document.createComment :
function testcmt() {
var el2 = document.createElement("div");
el2.setAttribute('id', 'oxe_rem_now' + '232323232323');
el2.appendChild(document.createComment('text for comment'));
var p = document.createElement("p");
p.innerText = "Sample";
el2.appendChild(p);
alert(el2.innerHTML);
}
这篇关于在innerHTML中插入评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文