JS库之Highlight.js的用法详解


Posted in Javascript onSeptember 13, 2017

官网:https://highlightjs.org/

下载地址:https://highlightjs.org/download/

下载到本地后,新建个页面测试

1、在head中加入css和js的引用

<head> 
  <title>highlight</title> 
  <meta http-equiv="content-type" content="text/html;charset=utf-8"> 
  <link rel="stylesheet" href="styles/default.css" rel="external nofollow" rel="external nofollow" > 
  <script src="highlight.pack.js"></script> 
  <script>hljs.initHighlightingOnLoad();</script> 
</head>

2、添加对应要显示的内容

<pre> 
<code class="python"> 
  # 读取文件内容 
  def fread(self): 
    # 如果指针位置大于文件大小,说明是更换了文件 
    self.filename = self.getLogPath() 
    if not os.path.isfile(self.filename): 
      while not self.checkfile() : 
        time.sleep(5) 
        self.filename = self.getLogPath() 
      self.fclose() 
      self.fopen() 
      self.pos = 0 
    self.hd.seek(self.pos,0) 
    fline = self.hd.readline() 
    self.pos = self.hd.tell() 
    return fline.replace("\n","") 
  # 将文件指针定位到文件尾部 
  def feof(self): 
    self.fopen() 
    # 定位到文件末尾 
    self.hd.seek(0,2) 
    #设置指针位置 
    self.pos = self.hd.tell() 
</code> 
</pre>

这里需要把要显示的内容放入到  <pre><code>这里放内容...</code></pre>  这种格式中

默认的话会智能识别,若是识别不出来是什么语言,就需要在<code>标签中加入class

例如 <pre><code class="java" >xxx</code></pre> 即可

下载的时候可以选择你需要的语言,然后再打包,下载后会发现有个style文件夹,里面放了各种不同的显示颜色,为了看看都是些什么样子

我这里弄了个可以选择样式的页面

代码如下

<html> 
<head> 
  <title>highlight</title> 
  <meta http-equiv="content-type" content="text/html;charset=utf-8"> 
  <link rel="stylesheet" href="styles/default.css" rel="external nofollow" rel="external nofollow" > 
  <script src="highlight.pack.js"></script> 
  <script>hljs.initHighlightingOnLoad();</script> 
</head> 
<body> 
<div id="changeStyleSelect"></div> 
PHP 
<hr> 
<pre><code class="php"> 
class Test { 
  private $name; 
  private $age; 
  public function __construct($array) { 
    $this->name = $array['name']; 
    $this->age = $array['age']; 
  } 
  public function getName() { 
    return $this->name; 
  } 
  public function setName($name) { 
    $this->name = $name; 
  } 
  public function getAge() { 
    return $this->age; 
  } 
  public function show() { 
    printf('my name is %s,age is %d',$this->name,$this->age); 
  } 
} 
</code></pre> 
javascript 
<hr> 
<pre> 
<code class="javascript"> 
  test = { 
    name : function () { 
      return 'hello'; 
    }, 
    age : function () { 
      return 13; 
    } 
  } 
</code> 
</pre> 
python 
<hr> 
<pre> 
<code class="python"> 
  # 读取文件内容 
  def fread(self): 
    # 如果指针位置大于文件大小,说明是更换了文件 
    self.filename = self.getLogPath() 
    if not os.path.isfile(self.filename): 
      while not self.checkfile() : 
        time.sleep(5) 
        self.filename = self.getLogPath() 
      self.fclose() 
      self.fopen() 
      self.pos = 0 
    self.hd.seek(self.pos,0) 
    fline = self.hd.readline() 
    self.pos = self.hd.tell() 
    return fline.replace("\n","") 
  # 将文件指针定位到文件尾部 
  def feof(self): 
    self.fopen() 
    # 定位到文件末尾 
    self.hd.seek(0,2) 
    #设置指针位置 
    self.pos = self.hd.tell() 
</code> 
</pre> 
java 
<hr> 
<pre> 
<code class="java"> 
  package l2f.gameserver.model; 
  import java.util.ArrayList; 
  public abstract class L2Character extends L2Object { 
   public static final Short ABNORMAL_EFFECT_BLEEDING = 0x0_0_0_1; // not sure 
   public void moveTo(int x, int y, int z) { 
    _ai = null; 
    _log.warning("Should not be called"); 
    if (1 > 5) { 
     return; 
    } 
   } 
   /** Task of AI notification */ 
   @SuppressWarnings( { "nls", "unqualified-field-access", "boxing" }) 
   public class NotifyAITask implements Runnable { 
    private final CtrlEvent _evt; 
    List mList = new ArrayList() 
    public void run() { 
     try { 
      getAI().notifyEvent(_evt, _evt.class, null); 
     } catch (Throwable t) { 
      t.printStackTrace(); 
     } 
    } 
   } 
  } 
</code> 
</pre>   
<script> 
  styleArr = ["agate.css","androidstudio.css","arduino-light.css","arta.css","ascetic.css","atelier-cave-dark.css","atelier-cave-light.css","atelier-dune-dark.css","atelier-dune-light.css","atelier-estuary-dark.css","atelier-estuary-light.css","atelier-forest-dark.css","atelier-forest-light.css","atelier-heath-dark.css","atelier-heath-light.css","atelier-lakeside-dark.css","atelier-lakeside-light.css","atelier-plateau-dark.css","atelier-plateau-light.css","atelier-savanna-dark.css","atelier-savanna-light.css","atelier-seaside-dark.css","atelier-seaside-light.css","atelier-sulphurpool-dark.css","atelier-sulphurpool-light.css","brown-paper.css","codepen-embed.css","color-brewer.css","dark.css","darkula.css","default.css","docco.css","dracula.css","far.css","foundation.css","github.css","github-gist.css","googlecode.css","grayscale.css","gruvbox-dark.css","gruvbox-light.css","hopscotch.css","hybrid.css","idea.css","ir-black.css","kimbie.dark.css","kimbie.light.css","magula.css","mono-blue.css","monokai.css","monokai-sublime.css","obsidian.css","paraiso-dark.css","paraiso-light.css","pojoaque.css","purebasic.css","qtcreator_dark.css","qtcreator_light.css","railscasts.css","rainbow.css","school-book.css","solarized-dark.css","solarized-light.css","sunburst.css","tomorrow.css","tomorrow-night.css","tomorrow-night-blue.css","tomorrow-night-bright.css","tomorrow-night-eighties.css","vs.css","xcode.css","xt256.css","zenburn.css"]; 
  selectHtml = []; 
  selectHtml.push('<select id="changeStyle">'); 
  for(i in styleArr) { 
    OptionValue = styleArr[i]; 
    selectHtml.push('<option value="' + OptionValue +'" >'+ OptionValue +'</option>'); 
  } 
  selectHtml.push('</select>'); 
  selectHtmlString = selectHtml.join(""); 
  document.getElementById('changeStyleSelect').innerHTML = selectHtmlString; 
  obj = document.getElementById('changeStyle'); 
  obj.addEventListener("change",function(event){ 
    var value = this.options[this.options.selectedIndex].value;   
    l = document.createElement('link'); 
    l.setAttribute('href','styles/'+value); 
    l.setAttribute('rel','stylesheet'); 
    document.head.appendChild(l); 
  }); 
  </script> 
</body> 
</html>

效果图:

JS库之Highlight.js的用法详解

JS库之Highlight.js的用法详解

总结

以上所述是小编给大家介绍的JS库之Highlight.js的用法详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
JScript中的undefined和&quot;undefined&quot;的区别
Mar 08 Javascript
Jquery知识点三 jquery表单对象操作
Jan 17 Javascript
一个简单的网站访问JS计数器 刷新1次加1次访问
Sep 20 Javascript
Javascript中的for in循环和hasOwnProperty结合使用
Jun 05 Javascript
浅析jQuery中调用ajax方法时在不同浏览器中遇到的问题
Jun 11 Javascript
JQuery菜单效果的两个实例讲解(3)
Sep 17 Javascript
javascript类型系统——undefined和null全面了解
Jul 13 Javascript
JavaScript toUpperCase()方法使用详解
Aug 26 Javascript
JavaScript查看代码运行效率console.time()与console.timeEnd()用法
Jan 18 Javascript
Bootstrap简单实用的表单验证插件BootstrapValidator用法实例详解
Mar 29 Javascript
JS+Canvas实现五子棋游戏
Aug 26 Javascript
vue 导航守卫和axios拦截器有哪些区别
Dec 19 Vue.js
详解wow.js中各种特效对应的类名
Sep 13 #Javascript
强大的JavaScript响应式图表Chartist.js的使用
Sep 13 #Javascript
JS库之Waypoints的用法详解
Sep 13 #Javascript
JS库particles.js创建超炫背景粒子插件(附源码下载)
Sep 13 #Javascript
详解React Native开源时间日期选择器组件(react-native-datetime)
Sep 13 #Javascript
基于vue的短信验证码倒计时demo
Sep 13 #Javascript
BootStrap数据表格实例代码
Sep 13 #Javascript
You might like
深入php之规范编程命名小结
2013/05/15 PHP
php加密解密实用类分享
2014/01/07 PHP
php实现图形显示Ip地址的代码及注释
2014/01/20 PHP
推荐10个提供免费PHP脚本下载的网站
2014/12/31 PHP
Laravel 5框架学习之表单
2015/04/08 PHP
解读PHP的Yii框架中请求与响应的处理流程
2016/03/17 PHP
JScript内置对象Array中元素的删除方法
2007/03/08 Javascript
Javascript常用运算符(Operators)-javascript基础教程
2007/12/14 Javascript
JQuery 国际象棋棋盘 实现代码
2009/06/26 Javascript
jquery下将选择的checkbox的id组成字符串的方法
2010/11/28 Javascript
javascript检测浏览器flash版本的实现代码
2011/12/06 Javascript
JavaScript中for..in循环陷阱介绍
2013/11/12 Javascript
浅谈Javascript 执行顺序
2013/12/18 Javascript
在JavaScript中判断整型的N种方法示例介绍
2014/06/18 Javascript
超级简单实现JavaScript MVC 样式框架
2015/03/24 Javascript
javascript实现二级级联菜单的简单制作
2015/11/19 Javascript
js滚动条平滑移动示例代码
2016/03/29 Javascript
JavaScript代码执行的先后顺序问题
2017/10/29 Javascript
VSCode插件安装完成后的配置(常用配置)
2020/08/24 Javascript
python3使用scrapy生成csv文件代码示例
2017/12/28 Python
python实现读取大文件并逐行写入另外一个文件
2018/04/19 Python
python3读取csv文件任意行列代码实例
2020/01/13 Python
TFRecord文件查看包含的所有Features代码
2020/02/17 Python
浅谈基于Canvas的手绘风格图形库Rough.js
2018/03/19 HTML / CSS
全球异乡人的跨境社交电商平台:Kouhigh口嗨网
2020/07/24 全球购物
电子邮箱格式怎么写
2014/01/12 职场文书
教师党性分析材料
2014/02/04 职场文书
乡镇群众路线教育实践活动整改措施
2014/10/04 职场文书
参观邀请函范文
2015/02/02 职场文书
工作会议通知
2015/04/15 职场文书
大一新生军训新闻稿
2015/07/17 职场文书
吧主申请感言怎么写
2015/08/03 职场文书
公司致全体员工的感谢信
2019/06/24 职场文书
python如何做代码性能分析
2021/04/26 Python
vue-router中hash模式与history模式的区别
2021/06/23 Vue.js
微软团队与 NASA 科学家和惠普企业(HPE)的工程师合作
2022/04/21 数码科技