如何在向 html 源代码添加行号时,使用 highlight.js
使用 Highlight.js 能够让你给代码块添加行号,但你需要执行一些额外步骤。
正确的方法:
将以下样式添加到你的
标签中:
pre { position: relative; margin-bottom: 24px; border-radius: 3px; border: 1px solid #C3CCD0; background: #FFF; overflow: hidden;}code { display: block; padding: 12px 24px; overflow-y: auto; font-weight: 300; font-family: Menlo, monospace; font-size: 0.8em;}code.has-numbering { margin-left: 21px;}.pre-numbering { position: absolute; top: 0; left: 0; width: 20px; padding: 12px 2px 12px 0; border-right: 1px solid #C3CCD0; border-radius: 3px 0 0 3px; background-color: #EEE; text-align: right; font-family: Menlo, monospace; font-size: 0.8em; color: #AAA;}
登录后复制
然后,在 HTML 中,将 class=”language-html” 添加到你的代码块中,并在代码块的父标签的末尾添加以下代码:
立即学习“前端免费学习笔记(深入)”;
$(function () { $('pre code').each(function () { var lines = $(this).text().split('').length - 1; var $numbering = $('
- ').addClass('pre-numbering'); $(this) .addClass('has-numbering') .parent() .append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('
- ').text(i)); } });});
登录后复制
这样,你的代码块就会带有数字行号。
以上就是如何使用 Highlight.js 在 HTML 代码块中添加行号?的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2806481.html