1

<pre class="prettyprint">如果存在,我只会加载脚本。我发现要启动 Prettify,您需要使用prettyPrint();.

但是,在不加载脚本的页面上,我收到 Javascript 错误。有没有办法测试 Prettify 是否已加载,然后运行...否则什么都不做?

4

3 回答 3

0

This should work

function getElementsByClassName(className) {
if (document.getElementsByClassName) { 
    return document.getElementsByClassName(className); }
else { return document.querySelectorAll('.' + className); } }

window.onload = function() {
    if (getElementsByClassName('prettyprint').length) {
        /* element exists */
    } else { /* element not exists */ }
}

[EDIT] Ooops, you asked for the function existance, my bad :)

于 2013-03-07T06:00:20.460 回答
0

你不需要再打电话prettyPrint了。

https://code.google.com/p/google-code-prettify/wiki/GettingStarted

自动装载机

您可以通过一个 URL 加载 JavaScript 和 CSS 进行美化

<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>

将加载整个系统并安排美化器在页面加载时运行。您可以指定多种附加选项(作为 CGI 参数)来配置运行器。

...

于 2013-03-07T06:09:04.880 回答
0

你可以像这样测试它:

if (window.prettyPrint) {
    prettyPrint();
}
于 2013-03-07T05:45:22.680 回答