首页 > 解决方案 > 在 html 内容中显示 html 标签,在 html 中显示 html 代码片段

问题描述

我想知道是否有一种方法可以应用htmlentities()var x这样如果我将<p>hello</p>放入text area并按下按钮,它将使用以下代码 显示<p>hello</p>而不是hello 。

提前致谢

function myFunction1() {
  var x = document.getElementById("myText1").value;
  document.getElementById("demo1").innerHTML = x;
}
<textarea name="myText" id="myText1" type="text"></textarea>
<button onclick="myFunction1()">thing</button>
<p><span id="demo1"></span></p>

标签: javascriptphphtmltextareahtml-entities

解决方案


你需要使用<xmp>标签

<xmp>
  <p>hello</p>
</xmp>

function myFunction1() {
  var x = document.getElementById("myText1").value;
  document.getElementById("demo1").innerHTML = x;
}
<textarea name="myText" id="myText1" type="text"></textarea>
<button onclick="myFunction1()">Show</button>
<p><xmp id="demo1"></xmp></p>


推荐阅读