如何向 Google Prettify 中的所有行添加行号?

发布于 2024-12-19 21:46:42 字数 540 浏览 5 评论 0原文

我正在使用 prettify:

<pre class="prettyprint linenums">
  some code
</pre>

它可以工作,但行号每 5 行显示一次,而不是每行显示一次。 使用这些文件

<link href="../src/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../src/prettify.js"></script>

我基本上在本页末尾 http://google-code- prettify.googlecode.com/svn/trunk/styles/index.html 你可以看到我想要的,但我查看了该代码,但无法弄清楚。

I am using prettify:

<pre class="prettyprint linenums">
  some code
</pre>

It works but the line number show every 5 lines and not for every line. I am using these files

<link href="../src/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../src/prettify.js"></script>

Basically at the end of this page http://google-code-prettify.googlecode.com/svn/trunk/styles/index.html you can see that I want, but I looked at that code and I can't figure it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风筝在阴天搁浅。 2024-12-26 21:46:42

根本原因是 prettify.css 中的 list-style-type: none

/* Specify class=linenums on a pre to get line numbering */
ol.linenums { margin-top: 0; margin-bottom: 0 } /* IE indents via margin-left */
li.L0,
li.L1,
li.L2,
li.L3,
li.L5,
li.L6,
li.L7,
li.L8 { list-style-type: none /* <<< THIS is the cause! */ }
/* Alternate shading for lines */
li.L1,
li.L3,
li.L5,
li.L7,
li.L9 { background: #eee }

您可以删除该规则或使用以下命令覆盖它:

.linenums li {
    list-style-type: decimal;
}

The root cause is list-style-type: none in prettify.css:

/* Specify class=linenums on a pre to get line numbering */
ol.linenums { margin-top: 0; margin-bottom: 0 } /* IE indents via margin-left */
li.L0,
li.L1,
li.L2,
li.L3,
li.L5,
li.L6,
li.L7,
li.L8 { list-style-type: none /* <<< THIS is the cause! */ }
/* Alternate shading for lines */
li.L1,
li.L3,
li.L5,
li.L7,
li.L9 { background: #eee }

You can either remove that rule or override it with:

.linenums li {
    list-style-type: decimal;
}
放我走吧 2024-12-26 21:46:42

解决方案

您无需修改​​ CSS,只需添加一行 CSS 即可实现所需的行为:

<style>

    .prettyprint ol.linenums > li { list-style-type: decimal; }

</style>  

示例

完整的示例可能包含以下代码。 通过 jsfiddle 查看结果或参见下文

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css" />

    <style>
        .prettyprint ol.linenums > li { list-style-type: decimal; }
    </style>

<pre class="prettyprint linenums">
 foo
 bar
 bis
 sed
 awk
 cat
</pre>


<script src="//cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/prettify/r298/run_prettify.js"></script>

Solution

Instead of modifying the CSS you can simply add in a line of CSS to achieve the desired behavior:

<style>

    .prettyprint ol.linenums > li { list-style-type: decimal; }

</style>  

Example

A full example might have the code below. View results via jsfiddle or see below

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css" />

    <style>
        .prettyprint ol.linenums > li { list-style-type: decimal; }
    </style>

<pre class="prettyprint linenums">
 foo
 bar
 bis
 sed
 awk
 cat
</pre>


<script src="//cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/prettify/r298/run_prettify.js"></script>

ゞ记忆︶ㄣ 2024-12-26 21:46:42

我喜欢交替的背景颜色,所以这样做:

/* White background color for all even-numbered lines */
li.L0,
li.L2,
li.L4,
li.L6,
li.L8  { background-color: #fff; }
/* Light-gray background color for all odd-numbered lines */
li.L1,
li.L3,
li.L5,
li.L7,
li.L9 { background-color: #eee; }

I like having the alternating background colors, so did it this way:

/* White background color for all even-numbered lines */
li.L0,
li.L2,
li.L4,
li.L6,
li.L8  { background-color: #fff; }
/* Light-gray background color for all odd-numbered lines */
li.L1,
li.L3,
li.L5,
li.L7,
li.L9 { background-color: #eee; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文