SyntaxHighlighter是一個使用JavaScript編寫的功能齊全的代碼語法高亮的js插件。
查看效果
效果2
使用方法:
首先引用js文件:
<script type="text/javascript" src="http://hovertree.com/texiao/js/29/syntaxhighlighter/scripts/shCore.js"></script>
然後引用對應語言的格式js文件,例如js格式:
<script type="text/javascript" src="http://hovertree.com/texiao/js/29/syntaxhighlighter/scripts/shBrushJScript.js"></script>
C#格式的為:
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushCSharp.js"></script>
然引入樣式:
<link type="text/css" rel="stylesheet" href="http://hovertree.com/texiao/js/29/syntaxhighlighter/styles/shCoreDefault.css" />
然後寫一句代碼:
<script type="text/javascript">SyntaxHighlighter.all();</script>
然後是要著色的代碼框架:
<pre class="brush: js;">
</pre>
把要著色的代碼放在框架裡,
其中 brush:js 表示放在中間的是js代碼
如果是c#代碼則為:brush:csharp
js的例子如下:
<pre class="brush: js;">
var year = now.getFullYear();
var month = now.getMonth();
var date = now.getDate();
document.write("何問起年月日:"+year+" "+month+" "+date);
</pre>
這樣就實現的第一個效果了。查看效果
完整代碼如下:<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>使用SyntaxHighlighter著色,使代碼高亮_何問起</title><base target="_blank" />
<script type="text/javascript" src="http://hovertree.com/texiao/js/29/syntaxhighlighter/scripts/shCore.js"></script>
<script type="text/javascript" src="http://hovertree.com/texiao/js/29/syntaxhighlighter/scripts/shBrushJScript.js"></script>
<link type="text/css" rel="stylesheet" href="http://hovertree.com/texiao/js/29/syntaxhighlighter/styles/shCoreDefault.css" />
<script type="text/javascript">SyntaxHighlighter.all();</script>
<style>.hovertreeinfo{
max-width:800px;
}.hovertreeinfo a{color:blue}</style>
</head>
<body style="background: white; font-family: Helvetica">
<h1>使用SyntaxHighlighter著色,使代碼高亮</h1>
下面是JavaScript代碼
<div class="hovertreeinfo">
<pre class="brush: js;">
var year = now.getFullYear();
var month = now.getMonth();
var date = now.getDate();
document.write("何問起年月日:"+year+" "+month+" "+date);
</pre>
<a href="http://hovertree.com">首頁</a>
<a href="http://hovertree.com/texiao/">特效</a>
<a href="http://hovertree.com/h/bjaf/8m6o9n4n.htm">代碼說明</a>
</div>
</body>
</html>
各種語言的名稱和文件如下:
Brush name | Brush aliases | File name |
ActionScript3as3, actionscript3shBrushAS3.js
Bash/shellbash, shellshBrushBash.js
ColdFusioncf, coldfusionshBrushColdFusion.js
C#c-sharp, csharpshBrushCSharp.js
C++cpp, cshBrushCpp.js
CSScssshBrushCss.js
Delphidelphi, pas, pascalshBrushDelphi.js
Diffdiff, patchshBrushDiff.js
Erlangerl, erlangshBrushErlang.js
GroovygroovyshBrushGroovy.js
JavaScriptjs, jscript, javascriptshBrushJScript.js
JavajavashBrushJava.js
JavaFXjfx, javafxshBrushJavaFX.js
Perlperl, plshBrushPerl.js
PHPphpshBrushPhp.js
Plain Textplain, textshBrushPlain.js
PowerShellps, powershellshBrushPowerShell.js
Pythonpy, pythonshBrushPython.js
Rubyrails, ror, rubyshBrushRuby.js
ScalascalashBrushScala.js
SQLsqlshBrushSql.js
Visual Basicvb, vbnetshBrushVb.js
XMLxml, xhtml, xslt, html, xhtmlshBrushXml.js
代碼托管:https://github.com/syntaxhighlighter/syntaxhighlighter