javascript - 在 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>
解决方案
你需要使用<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>
推荐阅读
- xml - 嵌套的 XML/JSON 结构标签,定义结构的正确方法是什么?
- python - 使用正则表达式删除字符串中的数字
- python-3.x - 使用 AND、OR、NOT 交换变量值的代码是什么
- firebase - 使用由第三方控制的 SSO 的 Firebase (Firestore) 自定义身份验证
- laravel - Laravel 工作中未定义的属性
- php - 检查已通过 10 分钟
- entity-framework - 实体框架中的蛮力收集
- ios - 如何从另一个 ViewController 检索 UseDefault 保存的数据
- swift - 给定 XCUITest 中的文本,如何获取单元格的索引?
- python - 将多个正则表达式与单个列匹配(并将匹配项制成表格)