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中escape对应的C#解码函数 UrlDecode
Dec 16 Javascript
jquery 层次选择器siblings与nextAll的区别介绍
Aug 02 Javascript
JavaScript实现继承的4种方法总结
Oct 16 Javascript
JavaScript字符串对象substr方法入门实例(用于截取字符串)
Oct 16 Javascript
浅谈javascript中的三种弹窗
Oct 21 Javascript
Angular 4.x 动态创建表单实例
Apr 25 Javascript
AngularJS自定义指令之复制指令实现方法
May 18 Javascript
vue一步步实现alert功能
Jul 05 Javascript
基于BootStrap的文本编辑器组件Summernote
Oct 27 Javascript
JS获取指定月份的天数两种实现方法
Jun 22 Javascript
JS实现移动端双指缩放和旋转方法
Dec 13 Javascript
Vue表单控件数据绑定方法详解
Feb 05 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
用php实现像JSP,ASP里Application那样的全局变量
2007/01/12 PHP
PHP下escape解码函数的实现方法
2010/08/08 PHP
php smarty截取中文字符乱码问题?gb2312/utf-8
2011/11/07 PHP
Android ProgressBar进度条和ProgressDialog进度框的展示DEMO
2013/06/19 PHP
PHP实现生成带背景的图形验证码功能
2016/10/03 PHP
thinkPHP订单数字提醒功能的实现方法
2016/12/01 PHP
PHP 验证身份证是否合法的函数
2017/02/09 PHP
javascript 获取网页参数系统
2008/07/19 Javascript
优化 JavaScript 代码的方法小结
2009/07/16 Javascript
Javascript图像处理思路及实现代码
2012/12/25 Javascript
js日期时间补零的小例子
2013/03/05 Javascript
JSON 数字排序多字段排序介绍
2013/09/18 Javascript
JQuery与JS里submit()的区别示例介绍
2014/02/17 Javascript
JavaScript模拟鼠标右键菜单效果
2020/12/08 Javascript
在Docker快速部署Node.js应用的详细步骤
2016/09/02 Javascript
jQuery内容过滤选择器用法示例
2016/09/09 Javascript
thinkjs之页面跳转同步异步操作
2017/02/05 Javascript
JavaScript实现的冒泡排序法及统计相邻数交换次数示例
2017/04/26 Javascript
微信小程序生成分享海报方法(附带二维码生成)
2019/03/29 Javascript
Layui弹出层 加载 做编辑页面的方法
2019/09/16 Javascript
flexible.js实现移动端rem适配方案
2020/04/07 Javascript
基于JS+HTML实现弹窗提示是否确认提交功能
2020/06/17 Javascript
Python分析学校四六级过关情况
2017/11/22 Python
tensorflow获取变量维度信息
2018/03/10 Python
tensorflow 中对数组元素的操作方法
2018/07/27 Python
python opencv如何实现图片绘制
2020/01/19 Python
Python基于read(size)方法读取超大文件
2020/03/12 Python
jupyter notebook远程访问不了的问题解决方法
2021/01/11 Python
详解如何通过H5(浏览器/WebView/其他)唤起本地app
2017/12/11 HTML / CSS
Uber Eats台湾:寻找附近提供送餐服务的餐厅
2018/05/07 全球购物
2016年寒假社会实践活动总结
2015/03/27 职场文书
2016年领导干部正风肃纪心得体会
2015/10/09 职场文书
PHP对接阿里云虚拟号的实现(号码隐私保护)
2021/04/06 PHP
golang 实现时间戳和时间的转化
2021/05/07 Golang
Redis实战高并发之扣减库存项目
2022/04/14 Redis
配置nginx负载均衡
2022/05/06 Servers