带有 html 代码的 highlightjs [英] highlightjs with html code

查看:25
本文介绍了带有 html 代码的 highlightjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何放置我的 HTML 代码以便 highlight.js 美化它?

我试过了

<代码><!-- HTML 美化 --><div><pre class="pre-code-ception"><code>哈哈</code></pre>

我确实放在了我的文件末尾:

但一切都显示为纯 HTML.

解决方案

哦,我想我明白这个问题了.您需要对 元素中的 HTML 进行转义,否则它将被解释为 HTML 而不是文本(您希望 HTML 字面显示,而不是解释为网页结构的一部分).

将每个 < 更改为 &lt; 并将 > 更改为 &gt;,如以及代码示例中的任何其他特殊 HTML 字符.(如果您正在即时生成页面,大多数语言都有一个实用函数来为您转义 HTML.)

How do I put my HTML code so that highlight.js prettify it ?

I tried

<pre>
    <code>
        <!-- HTML Prettify -->
        <div>
            <pre class="pre-code-ception"><code> haha </code></pre>
        </div>
    </code>
</pre>

I did put at the end of my file :

<script type="text/javascript">
    hljs.initHighlightingOnLoad();
</script>

But everything is shown as plain HTML.

解决方案

Oh, I think I understand the problem. You need to escape the HTML within the <code> element, otherwise it will be interpreted as HTML instead of text (you want the HTML displayed literally, not interpreted as part of the webpage structure).

Change every < to &lt; and > to &gt;, as well as any other special HTML characters in your code sample. (If you're generating the page on the fly, most languages have a utility function to escape the HTML for you.)

这篇关于带有 html 代码的 highlightjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆