JS来显示HTML标记
JS to show HTML tags
例如,我有一个测试页面,如下所示:
index.html:
<p>This is how you should write paragraphs in HTML:</p>
<div id="example">
<p>Foo</p>
</div>
如果我在浏览器中打开这个页面,我想看到文本和标签:
This is how you should write paragraphs in HTML:
<p>Foo</p>
如何使用js自动完成?
是的,我可以用<
和>
手动替换<
和>
,但我想避免手动工作。
尝试htmlEncode
方法作为
function htmlEncode ( html )
{
html = $.trim(html);
return html.replace(/[&"''<'>]/g, function(c)
{
switch (c)
{
case "&":
return "&";
case "'":
return "'";
case '"':
return """;
case "<":
return "<";
default:
return ">";
}
});
};
现在将值编码为
$( "#example" ).html( htmlEncode ( $( "#example" ).html() ) );
演示
function htmlEncode ( html )
{
html = $.trim(html);
return html.replace(/[&"''<'>]/g, function(c)
{
switch (c)
{
case "&":
return "&";
case "'":
return "'";
case '"':
return """;
case "<":
return "<";
default:
return ">";
}
});
};
$( "#example" ).html( htmlEncode ( $( "#example" ).html() ) );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>This is how you should write paragraphs in HTML:</p>
<div id="example">
<p>Foo</p>
</div>
这里已经回答了这个问题:是否有一种HTML/CSS方法可以在不进行解析的情况下显示HTML标记?
<style>
script
{
display: block;
}
</style>
<div id="example">
Then within document body:
<script type="text/plain">
<p>Foo</p>
</script>
</div>
JSfiddle:https://jsfiddle.net/9vkowqb3/
检查以下jsfiddleHTML:
<p>This is how you should write paragraphs in HTML:</p>
<div id="example">
</div>
JS:
var text = document.createTextNode('<p>Stuff</p>');
document.getElementById('example').appendChild(text);
相关文章:
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- HTML标记在脚本标记中工作
- 是否可以添加这行“;“照原样”;在HTML标记中
- HTML锚标记无法在android平台中下载文件
- 一个正则表达式,用于从JS中的HTML标记中删除id、样式和类属性
- html文件中的script标记根本不起作用
- 如果匹配项在特定的html标记中,则跳过regex匹配
- 使用 Pure JS 更改 HTML 标记名称
- 在Android WebView中通过javascript检测点击HTML选项标记
- 上传文件时从 html 中删除脚本标记.html
- 在表单标记(HTML,JavaScript)中插入
- 无法将 XML 数据加载到表标记 Html
- 将具有相同ID类和标记HTML的多个元素拖放到屏幕上的任何位置
- 不允许在输入标记html中键入特定的单词
- 创建一个字段以接受标记HTML/JS
- 将文本插入锚定标记html
- 如何用灰色标记html范围滑块(所有已选择的区域)
- 加载谷歌地图标记HTML的要求
- 如何从jquery中获取dl标记HTML的值
- 从javascript中获取其他字段的值,javascript是select标记html的一部分