gpt4 book ai didi

javascript - 在 innerHTML 中插入注释

转载 作者:行者123 更新时间:2023-11-30 13:07:52 24 4
gpt4 key购买 nike

我正在尝试在 innerHTML 中插入 HTML 注释,但它在 IE8 中不被接受。 FF 工作正常。

<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>评论不见了。有什么指点吗?

最佳答案

您很可能应该使用 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);
}

Sample

关于javascript - 在 innerHTML 中插入注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15006001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com