如何在 HTML 文档中显示 html 标签?
我只是想创建类似的东西:
<strong>blah blah</strong>
我已经尝试这样做很长时间了,但由于某种原因,每当我放置代码和预标记时,它们都不会做他们应该做的事情。 他们确实取消了内容的格式 - 我没有看到任何格式,尽管标签没有出现。例如,我写道:
<pre><b>abc</b></pre>
abc 显示不是粗体,但我看不到标签本身。我怎样才能做到这一点? 我不想使用像 &fdaf; 这样的实体。因为我没有写传入的文本
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用
标记,例如您还可以在
内添加样式和格式。Use the
<xmp>
tag, e.g.You can also add styles and formatting inside
<xmp>
as well.如何用
<
和>
替换传入文本 标签应该在源视图中可见,但在“普通视图”中不可见。How about replacing the incoming texts by
<
and>
The tags should be visible in source view, but not "in normal view".您可以使用一些 JavaScript 来动态更改元素。
如果你使用jQuery:
就可以了。
You could use some javascript to change elements on the fly.
If you use jQuery:
does the trick.
正如 mkluwe 所说,您应该使用 JavaScript(或服务器端脚本)来正确转义任何用户输入。这是您可以尝试的另一个 JavaScript 函数:
我还没有在 IE 中测试过它,但理论上它应该可以在 IE 中工作。您可以在以下页面查看结果:http://jsbin.com/oruri5/2/edit
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:
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了。还有其他几个类似的代码 &符号等。
文本从哪里来?如果它是在 javascript 中,那么最简单的方法是在 javascript 中进行正则表达式替换。
The easiest way I know is to replace the < and > with < and > that way it's not html anymore. There are several other codes like these for & symbols, etc.
Where is the text coming from? If it's in javascript than you're easiest way would be to do a regex replace in javascript.
你必须写一些像&fdaf;这样的东西。因为浏览器拒绝相信您的意思是
You have to write stuff like &fdaf; because the browser refuses to believe that you mean <pre> for everything. This is the correct behavior, else how would you ever escape from a <pre> section? You have to go through the doc and for every less-than sign substitute <. You do not have to do that for the greater-than sign or for ampersand (usually).
请勿使用
、
和
。按照 MDN 的建议使用:Don't use
<xmp>
,<plaintext>
and<listing>
. Use as suggested by MDN:为了以@mkluwe的答案为基础,这里有一个例子:
to build on @mkluwe's answer, here is an example: