hljs.initHighlightingOnLoad();不直接呈现页面

时间:2018-07-13 13:01:57

标签: javascript jquery syntax-highlighting

我正在关注highlight.js  美化我的代码的教程,

 <link rel="stylesheet" href="https://cdn.jsdelivr.net/highlight.js/latest/styles/github.min.css">
<script async src="https://cdn.jsdelivr.net/highlight.js/latest/highlight.min.js"></script> 


和脚本:

<script>
  hljs.initHighlightingOnLoad();
</script>

我测试了最小代码:

<pre>
<code class="python">
import this print("35*35")
</code>
</pre>

不幸的是,它没有用,

enter image description here

本教程提供了使用jQuery调用initHighlightingOnLoad的等效方法:

<script>
  // hljs.initHighlightingOnLoad();
  $(document).ready(function () {
    $('pre code').each(function (i, block) {
      hljs.highlightBlock(block);
    });
  });
</script>

这次运行正常,

enter image description here

本教程指出,它们是等效的,现在看来还不是,

我是JS的新手,希望从这个错误中学习,
另一种尝试,

<script>
  $(document).ready(function () {
    hljs.initHighlightingOnLoad();
  });
</script>

它有效,我不知道为什么。

0 个答案:

没有答案
相关问题