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 相关文章推荐
用js实现计算加载页面所用的时间
Apr 02 Javascript
关于js数组去重的问题小结
Jan 24 Javascript
jquery操作select元素和option的实例代码
Feb 03 Javascript
JS封装的自动创建表格的实现代码
Jun 15 Javascript
Angular动态添加、删除输入框并计算值实例代码
Mar 29 Javascript
Vue.js实战之使用Vuex + axios发送请求详解
Apr 04 Javascript
微信小程序实现点击按钮修改字体颜色功能【附demo源码下载】
Dec 05 Javascript
ng-repeat指令在迭代对象时的去重方法
Oct 02 Javascript
vue学习笔记五:在vue项目里面使用引入公共方法详解
Apr 04 Javascript
微信小程序上传图片并等比列压缩到指定大小的实例代码
Oct 24 Javascript
js prototype深入理解及应用实例分析
Nov 25 Javascript
介绍一下28个JS常用数组方法
May 06 Javascript
详解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
全国FM电台频率大全 - 20 广西省
2020/03/11 无线电
探讨多键值cookie(php中cookie存取数组)的详解
2013/06/06 PHP
memcache命令启动参数中文解释
2014/01/13 PHP
PHP对文件进行加锁、解锁实例
2015/01/23 PHP
解决laravel中日志权限莫名变成了root的问题
2019/10/17 PHP
TP5(thinkPHP5)框架使用ajax实现与后台数据交互的方法小结
2020/02/10 PHP
js window.onload 加载多个函数和追加函数详解
2014/01/08 Javascript
js如何判断用户是在PC端和还是移动端访问
2014/04/24 Javascript
JavaScript中自带的 reduce()方法使用示例详解
2016/08/10 Javascript
jQuery Easyui datagrid行内实现【添加】、【编辑】、【上移】、【下移】
2016/12/19 Javascript
Vue+axios实现统一接口管理的方法
2018/07/23 Javascript
JavaScript 性能提升之路(推荐)
2019/04/10 Javascript
关于element的表单组件整理笔记
2021/02/05 Javascript
Python中的函数式编程:不可变的数据结构
2018/10/08 Python
Python GUI布局尺寸适配方法
2018/10/11 Python
python3.6使用tkinter实现弹跳小球游戏
2019/05/09 Python
PyQt5+Caffe+Opencv搭建人脸识别登录界面
2019/08/28 Python
python实现将json多行数据传入到mysql中使用
2019/12/31 Python
Python图像处理库PIL的ImageFilter模块使用介绍
2020/02/26 Python
python自动下载图片的方法示例
2020/03/25 Python
Python3操作读写CSV文件使用包过程解析
2020/04/10 Python
python 爬虫爬取京东ps4售卖情况
2020/12/18 Python
Python3.9.0 a1安装pygame出错解决全过程(小结)
2021/02/02 Python
CSS3使用border-radius属性制作圆角
2014/12/22 HTML / CSS
使用HTML5 Canvas绘制直线或折线等线条的方法讲解
2016/03/14 HTML / CSS
HTML5中图片之间的缝隙完美解决方法
2017/07/07 HTML / CSS
HTML5 客户端数据库简易使用:IndexedDB
2019/12/19 HTML / CSS
标准的毕业生自荐信
2014/04/20 职场文书
工商管理专业毕业生求职信
2014/05/26 职场文书
化学专业大学生职业生涯规划范文
2014/09/13 职场文书
计划生育工作汇报
2014/10/28 职场文书
淘宝客服专员岗位职责
2015/04/07 职场文书
回复函格式及范文
2015/07/14 职场文书
大学新生入学感想
2015/08/07 职场文书
确保减税降费落地生根,用实实在在措施
2019/07/19 职场文书
Nginx 502 bad gateway错误解决的九种方案及原因
2022/08/14 Servers