Asp.net

如何在網頁上顯示程式碼範例,並使用類似 Stack Overflow 的漂亮語法樣式?

  • January 5, 2015

我希望能夠從數據庫中提取一些程式碼範例和/或將文本嵌入到網站中,然後在螢幕上以類似方式的程式碼格式化範例。雖然螢幕上的文字本身就很棒,但格式會讓它更加使用者友好。這是怎麼做到的?

我想要這個:

 public string MyString = “The Sample Text”;


看起來像:

public string MyString = "The Sample Text";

用答案編輯

我接受了Gortok 的建議並研究了Prettify.js,它的表現正是我所希望的。這是如何實現的。

<head runat="server">
   <script src="Scripts/prettify.js" type="text/javascript"></script>
   <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
   <link href="Scripts/prettify.css" rel="stylesheet" type="text/css"></link>
</head>
<body>
   <code class="prettyprint"> 
       public String HelloString = "New String"; 
   </code>  
   <script language="javascript"> 
       $().ready(function() { 
           prettyPrint(); 
       }); 
   </script> 
</body> 

Stack Overflow 使用Google 的prettify.js

我使用 Alex Gorbatchev 的 SyntaxHighlighter

http://alexgorbatchev.com/wiki/SyntaxHighlighter

它在 Yahoo Developer Network 上使用

http://developer.yahoo.com/yui/animation/

引用自:https://stackoverflow.com/questions/749209