Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:将html打印为纯文本_Javascript_Html - Fatal编程技术网

Javascript:将html打印为纯文本

Javascript:将html打印为纯文本,javascript,html,Javascript,Html,我正在尝试从javascript打印纯html,但不知道如何实现。 例如: $('#elem').html('This is a normal string'); // --> This is a normal string $('#elem').html('This <b>contains</b> html') //Current-> This contains html (contains is bold) //Wanted-> This &l

我正在尝试从javascript打印纯html,但不知道如何实现。 例如:

$('#elem').html('This is a normal string'); // --> This is a normal string

$('#elem').html('This <b>contains</b> html')
//Current-> This contains html   (contains is bold)
//Wanted-> This <b>contains</b> html
$('#elem').html('这是一个普通字符串');/>这是一个普通字符串
$('#elem').html('这包含html')
//当前->包含html(包含为粗体)
//想要->这包含html
我之所以想这样做,是因为我正在处理用户编写的注释,如果他们在注释中添加了一些html标记,我不希望应用这些标记。但出于其他原因,我需要查看标签

谢谢你的帮助

使用jQuery函数,它会自动将HTML转义为文本

在本例中,它是:

$('#elem').text('This <b>contains</b> html')
$('#elem').text('这包含html')
您可以使用jQuery方法

$('#elem').text('这包含html');
示例:


$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
$(“#elem”).text('这包含html');
});
});
点击我!!

这是一个段落


以下各项应起作用:

$('#elem').text('This <b>contains</b> html');
$('#elem').text('这包含html');
相关代码段:

$(“#一个id”).text('我不希望这是HTML')


这不是HTML

使用
.text
而不是
.html
$('#elem').text('This <b>contains</b> html');