简体   繁体   English

使用“ google-code-prettify”和动态DOM的尝试失败

[英]Unsuccessful attempt using “google-code-prettify” with dynamically DOM

Inside head tag I have: head标签中,我有:

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

Somewhere inside body tag I have: 我在体内标签的某个位置:

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

In my main css file I have some pre and code which should override prettify: 在我的主要css文件中,我有一些precode应该覆盖prettify:

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

Intentionally , I didn't include <body onload="prettyPrint()"> . 故意地 ,我没有包含<body onload="prettyPrint()"> The #code-context is hidden when the page is loaded so it's unnecessary. 加载页面时, #code-context是隐藏的,因此不必要。 The code become visible after the user trigger a jQuery function which goes: 用户触发jQuery函数后,代码将可见:

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

I'm getting the message in the console (which implies that all went OK), but in fact what I get is a border around all the code, and the code itself isn't color-coded. 我在控制台中收到消息(这意味着一切正常),但实际上我得到的是所有代码的边框,并且代码本身未进行颜色编码。

Help? 救命?

Found it! 找到了!

I used jQuery's get() instead of load() and it worked, though I don't fully understand why. 我使用了jQuery的get()而不是load() ,尽管我不完全理解为什么,但它仍然有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM