14

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

4

8 回答 8

25

使用<xmp>标签,例如

<xmp>
  <html>
    <body>This is my html inside html.</body>
  </html>
</xmp>

您也可以在里面添加样式和格式<xmp>

于 2013-06-01T01:59:30.827 回答
17

&lt;用和替换传入的文本怎么样&gt;?标签应该在源视图中可见,而不是“在普通视图中”。

于 2011-03-23T14:02:51.580 回答
9

您可以使用一些 javascript 动态更改元素。

如果你使用jQuery

$( 'pre' ).text( $( 'pre' ).html() );

成功了。

于 2011-03-23T14:15:55.887 回答
8

就像 mkluwe 还说的那样,您应该使用 JavaScript(或服务器端脚本)来正确转义任何用户输入。这是您可以尝试的另一个 JavaScript 函数:

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();
}

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

于 2011-03-23T14:25:52.560 回答
4

我知道的最简单的方法是将 < 和 > 替换为 < 和> 这样它就不再是html了。还有其他几个代码,例如 & 符号等。

文字从何而来?如果它在 javascript 中,那么最简单的方法是在 javascript 中进行正则表达式替换。

于 2011-03-23T14:03:16.540 回答
1

不要使用<xmp>,<plaintext><listing>. 按照MDN的建议使用:

使用<pre>元素,或者,如果语义足够,则使用<code>元素。请注意,您需要对<字符进行转义,&lt;以确保它不会被解释为标记。

于 2015-03-19T10:25:26.363 回答
1

必须写像 &fdaf; 这样的东西。因为浏览器拒绝相信你的意思是 <pre> 一切。这是正确的行为,否则您将如何逃离 <pre> 部分?您必须仔细阅读文档,并为每个小于号替换 <。对于大于号或 & 符号(通常),您不必这样做。

于 2011-03-23T14:09:06.893 回答
0

以@mkluwe 的回答为基础,这里有一个例子:

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">        </script>
  </head>
  <body>
    <pre>
      <a href="http://localhost:3000/l/1/2/3" target="_blank">
        <img src="http://localhost:3000/b/1/2/3/4" target="_blank">
      </a>
    </pre>
    <script>
      $( 'pre' ).text( $( 'pre' ).html() );
    </script>
  </body>
</html>
于 2015-08-13T20:04:48.437 回答