HTML<代码>中的颜色(语法突出显示)标签

发布于 2024-08-09 14:33:30 字数 389 浏览 6 评论 0原文

在像下面这样的代码片段中……

    class Foo
    {
      internal Foo()
      {
        for (int i = 0; i < 42; ++i);
      }
    }

它的各种关键字等在我的浏览器中显示时都是用颜色编码的。

当我执行“查看源代码”时,我在 HTML 中没有看到任何可以实现此颜色编码的特殊内容。

那么,这种语法特定的颜色突出显示是如何和/或在哪里实现的呢?例如,它是内置于浏览器中的,还是通过特定于站点的 JavaScript 在浏览器中编辑 DOM 来实现的?

我发现这个问题对谷歌来说很难回答。

In a code fragment like the following ...

    class Foo
    {
      internal Foo()
      {
        for (int i = 0; i < 42; ++i);
      }
    }

... its various keywords and so on are color-coded when they're displayed in my browsers.

When I do "View source", I don't see anything special in the HTML that would implement this color-coding.

How and/or where is this syntax-specific color-highlighting implemented, then? For example is it built-in to the browsers, or is it implemented by site-specific JavaScript editing the DOM within the browsers?

I find this a difficult question to Google for.

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

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

发布评论

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

评论(3

以酷 2024-08-16 14:33:30

Stack Overflow 使用 Google 的 prettify JS 库来进行语法突出显示。它在服务器传送 HTML 后在客户端执行。这就是为什么您在原始 HTML 源代码中看不到它的原因。如果您有浏览器插件,例如 FireBug,您将能够在 prettify 发挥其魔力后检查 DOM。

2020 年 9 月 14 日更新:Stack Overflow 从 Google 的 prettify 切换到highlight.js

Stack Overflow uses Google's prettify JS library for doing syntax highlighting. It executes on the client side after the HTML has been delivered by the server. That's why you don't see it in the raw HTML source. If you have a browser plugin such as FireBug, you'll be able to inspect the DOM after prettify has done its magic.

Update 2020-09-14: Stack Overflow switched from Google's prettify to highlight.js.

穿越时光隧道 2024-08-16 14:33:30

有一个很好的常见问题解答什么是语法高亮以及它是如何工作的?元SE。

这是相当广泛的。为了您的方便,我将引用与您的问题最相关的部分:


它是如何工作的?

创建或编辑帖子时,一旦您停止输入 5 秒,语法突出显示就会根据问题的标签分配给预览。

Stack Exchange 没有自己的语法突出显示引擎。它使用 highlight.js,并且可能不一定使用该库的最新版本。因此,Stack Exchange 无法处理有关语法突出显示的任何错误和功能请求。

为什么我的代码没有正确突出显示?

[继续阅读原帖...]

There is an excellent FAQ What is syntax highlighting and how does it work? over on meta.SE.

It’s pretty extensive. For your convenience, i’ll quote the part most related to your question:


How does it work?

When creating or editing posts, syntax highlighting is assigned to the preview based on question's tags as soon as you stop typing for 5 seconds.

Stack Exchange does not have its own syntax highlighting engine. It uses highlight.js, and may not necessarily be using the latest release of that library. Therefore, any bugs and feature requests regarding syntax highlighting cannot be handled by Stack Exchange.

Why isn't my code being highlighted correctly?

[ Continue reading the original post … ]

等数载,海棠开 2024-08-16 14:33:30

Stack Overflow 使用 Javascript 库。有很多,最流行的是 SyntaxHighlighter无效链接。不过,我个人最喜欢的是辣椒

Stack Overflow uses a Javascript library. There are quite a few out there, the most popular being SyntaxHighlighterDead link. My personal favorite is Chili, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文