Bootstrap代碼高亮樣式


今天Twitter發布Bootstrap 2.0,修復了許多Bug,增加了不少新樣式,同時Bootstrap的代碼高亮也是一大亮點,與其他網站的代碼高亮不同,淡淡的灰色調配上幾個鮮艷而不刺眼的亮色點綴,相當漂亮,看了源碼才知道,它是基於google-code-prettify的一套皮膚,於是我就決定把它搬到博客里來,下面是html、css、js的幾個demo。

  1. /*This is not code*/
  2. #site-generator .sep {
  3. background: url(images/wordpress.png) center left no-repeat;
  4. color: transparent;
  5. display:inline-block;
  6. height:16px;
  7. line-height:16px;
  8. margin:07px;
  9. width:16px;
  10. }
  1. /*This is not code*/
  2. <!DOCTYPE html>
  3. <htmllang="en"><head>
  4. <metacharset="utf-8">
  5. <title> imshanks | 我行我驌 </title>
  6. <linkrel="stylesheet"href="highlighter/prettify.css"/>
  7. </head>
  8. <body>
  9. <scriptsrc="highlighter/prettify.js"></script>
  10. </body>
  11. </html>
  1. /*This is not code*/
  2. $("#id").hover(function(){
  3. alert("imshanks.com");
  4. });

使用方法如下:

1)下載google-code-prettify
2)在head引入js和css文件

  1. <linkhref="google-code-prettify/prettify.css"type="text/css"rel="stylesheet"/>
  2. <scripttype="text/javascript"src="google-code-prettify/prettify.js"></script>

3)在body加上onload=”prettyPrint()

  1. <bodyonload="prettyPrint()">...</body>

如果你的主題里引入過Jquery.js,也可以加上下面這段代碼

  1. $(document).ready(function(){
  2. prettyPrint();
  3. })

設置方法如下

1)把代碼放進<pre>...</pre> 或者 <code>...</code> 就可以實行代碼高亮了。你也可以指定一種語言,class=”prettyprint Lang-html”,在lang- 后添加以下任何一種語言。“bash”, “c”, “cc”, “cpp”, “cs”, “csh”, “cyc”, “cv”, “htm”, “html”, ”java”, “js”, “m”, “mxml”, “perl”, “pl”, “pm”, “py”, “rb”, “sh”, ”xhtml”, “xml”, “xsl”

2)打開行號
你可以使用linenums打開行號

  1. <preclass=”prettyprintlinenumsLang-html>

喜歡這套樣式的童靴下載prettify.css替換就行了。
下面提供我改造的wordpress插件WP-Bootstrap-highlighting-of-code.rar用wordpress的童靴可以安裝插件直接設置並使用。

(此插件原版為http://boliquan.com/wp-code-highlight/開發,本人只做了樣式修改)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM