最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using Google Prettify to display HTML - Stack Overflow

programmeradmin3浏览0评论

for Google Prettify to display HTML code sample properly, you should replace all the < with &lt; and all the > with &gt;.

How do you automate that process using JavaScript only ?

for Google Prettify to display HTML code sample properly, you should replace all the < with &lt; and all the > with &gt;.

How do you automate that process using JavaScript only ?

Share Improve this question edited May 4, 2012 at 2:34 sarnold 104k23 gold badges185 silver badges243 bronze badges asked May 4, 2012 at 2:29 BrettBrett 1,7811 gold badge27 silver badges38 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 21

If you put your code inside an <xmp> element, you don't need to escape HTML special characters as shown in the tests

 <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.

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

评论列表(0)

  1. 暂无评论