我只在<pre class="prettyprint">存在的情况下加载脚本。我发现要启动Prettify,你需要使用prettyPrint();。
然而,在没有加载脚本的页面上,我得到了一个Javascript错误。有没有一种方法可以测试是否加载了Prettify,然后运行...否则什么都不做?
发布于 2013-03-07 13:45:23
您可以像这样测试它:
if (window.prettyPrint) {
prettyPrint();
}发布于 2013-03-07 14:00:20
这应该是可行的
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 */ }
}编辑哦,你要求的是函数的存在,我的错:)
发布于 2013-03-07 14:09:05
您不再需要调用prettyPrint。
https://code.google.com/p/google-code-prettify/wiki/GettingStarted说
自动加载器
你可以通过一个网址加载JavaScript和CSS,它将加载整个系统,并安排美容器在页面加载时运行。您可以指定各种附加选项(作为CGI参数)来配置运行器。
...
https://stackoverflow.com/questions/15263946
复制相似问题