尝试使用" google-code-prettify"动态DOM

时间:2016-04-20 14:09:07

标签: jquery html prettify google-code-prettify

我在head内部标记:

<link rel="stylesheet" href="google-code-prettify/prettify.css">
<script src="google-code-prettify/prettify.js"></script>

身体标签内的某处我有:

<div id='code-context'>
    <pre class="prettyprint"><code class="language-js"></code></pre>
</div>

在我的主css文件中,我有一些precode应该覆盖美化:

#code-context pre, #code-context code {
    font-family: 'Ubuntu Mono', monospace;
    width: 80ch;
    white-space: pre-wrap;
}

故意,我没有包含<body onload="prettyPrint()">。 加载页面时会隐藏#code-context,因此不需要。用户触发jQuery函数后代码变为可见:

$('#code-context').show();
var fileSource = "code-to-display.js";
$('code').load(fileSource);
$('.prettyprinted').removeClass('prettyprinted');
prettyPrint();
console.log('succeded!');

我在控制台中收到消息(这意味着一切都好了),但实际上我得到的是所有代码周围的边框,代码本身没有颜色编码。

帮助?

1 个答案:

答案 0 :(得分:0)

发现它!

我使用了jQuery&#39; get()代替load()并且它有效,但我不完全理解为什么。