我有一系列字符串,其中一些的格式类似于<p>Text here</p>
,而其他的格式则类似于“此处的文本”。我一直遇到的难题是我想要渲染字符串并实际上在字符串<p>Text here</p>
中包含p标签。但是,这导致文本被包装在html段落标签中。
基本上,我通过使用
document.getElementById("stringLocation").innerHtml = " <p>Text here</p>"
如何成功使用这些DOM元素分配给dom,并在字符串中而不是html中包含<p>
标签?
答案 0 :(得分:1)
分配给.textContent
属性而不是.innerHTML
属性,字符串将被解析为 text 而不是HTML(除了创建文本节点之外,不保证任何内容):
document.getElementById("stringLocation").textContent = " <p>Text here</p>"
<pre id="stringLocation"></pre>
答案 1 :(得分:1)