简体   繁体   English

使用Google Prettify显示HTML

[英]Using Google Prettify to display HTML

for Google Prettify to display HTML code sample properly, you should replace all the < with &lt; 要使Google Prettify正确显示HTML代码示例,您应该替换所有< with &lt; and all the > with &gt; 和所有>&gt; .

How do you automate that process using JavaScript only ? 如何仅使用JavaScript自动执行该过程?

If you put your code inside an <xmp> element, you don't need to escape HTML special characters as shown in the tests 如果将代码放在<xmp>元素中,则不需要转义HTML特殊字符,如测试中所示

  <h1>HTML using XMP</h1> <xmp class="prettyprint" id="htmlXmp" ><html> <head> <title>Fibonacci number</title> </head> <body> <noscript> <dl> <dt>Fibonacci numbers</dt> <dd>1</dd> <dd>1</dd> <dd>2</dd> <dd>3</dd> <dd>5</dd> <dd>8</dd> &hellip; </dl> </noscript> <script type="text/javascript"><!-- function fib(n) { var a = 1, b = 1; var tmp; while (--n >= 0) { tmp = a; a += b; b = tmp; } return a; } document.writeln(fib(10)); // --> </script> </body> </html> </xmp> 

You could do a global replace to the content using a RegEx. 您可以使用RegEx对内容进行全局替换。

var regex = new RegExp("<", "g");
console.log(content.replace(regex, "&lt;"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM