如何在HTML文档中显示HTML标签? [英] How can I display html tags inside an HTML document?

查看:120
本文介绍了如何在HTML文档中显示HTML标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想创建类似这样的内容:

 < strong>等等等等< / strong> 

我一直在尝试做这个很长一段时间,但由于某些原因,每当我把代码而且预标签他们不会做他们应该做的。
他们对内容进行非格式化 - 我没有看到任何格式,但标签没有出现。例如,我写道:

 < pre>< b> abc< / b>< / pre> 

abc显示为不是粗体,但我看不到标签本身。我怎样才能做到这一点?
我不想使用像&fdaf这样的实体;因为我没有写入传入的文本,所以你应该使用JavaScript(或者一个服务器端脚本)来解决这个问题。

以正确地逃避任何用户输入。这里有另一个JavaScript函数可以尝试:

$ p $ String.prototype.escapeHTML = function(){
return(
this.replace(/> / g,'& gt;')。
replace(/< / g,'& lt;')。
replace(// g ('code'){
codeEl('test');
if .innerHTML = codeEl.innerHTML.escapeHTML();
}

我没有测试过这在IE中,但它应该在理论上工作。下面是一个页面,您可以查看结果: http://jsbin.com/ oruri5 / 2 / edit


I simply want to create something like that:

<strong>blah blah</strong>

I've been trying to do this for a long time though for some reason whenever I put the code and the pre tags they don't do what they should. They do unformat the content - I don't see any formatting, though the tags don't appear. For example, I wrote :

<pre><b>abc</b></pre>

The abc is appearing not bold, but I can't see the tag itself. How can I do that? I don't want to use entities like &fdaf; because I am not writing the incoming text

解决方案

Like mkluwe also said, you should use JavaScript (or a server-side script) to properly escape any user input. Here's another JavaScript function you could try:

String.prototype.escapeHTML = function () {                                        
  return(                                                                 
    this.replace(/>/g,'&gt;').
         replace(/</g,'&lt;').
         replace(/"/g,'&quot;')
  );
};
var codeEl = document.getElementById('test');
if (codeEl) {
  codeEl.innerHTML = codeEl.innerHTML.escapeHTML();
}

I haven't tested this in IE, but it should work there in theory. Here's a page where you can view the results: http://jsbin.com/oruri5/2/edit

这篇关于如何在HTML文档中显示HTML标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆