【问题标题】:Unsuccessful attempt using "google-code-prettify" with dynamically DOM使用动态 DOM 的“google-code-prettify”尝试失败
【发布时间】:2016-04-20 14:09:07
【问题描述】:

head 标签内我有:

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

我在 body 标签内的某处:

<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;
}

故意,我没有包含&lt;body onload="prettyPrint()"&gt;#code-context 在页面加载时被隐藏,因此没有必要。在用户触发 jQuery 函数后,代码变得可见:

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

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

帮助?

【问题讨论】:

  • 是否正在加载 CSS 文件?
  • @gabe3886,我在问题中添加了信息,感谢您的帮助。谢谢!

标签: jquery html prettify google-code-prettify


【解决方案1】:

找到了!

我使用 jQuery 的 get() 而不是 load() 并且它有效,但我不完全理解为什么。

【讨论】:

    猜你喜欢
    • 2013-04-05
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多