我正在使用自己托管的Google的prettify.js和prettify.css文件。 這是一個JavaScript和CSS腳本,突出顯示<pre>
或<code>
標記內的源代碼片段。 可以在以下位置找到文檔: https : //github.com/google/code-prettify/blob/master/docs/getting_started.md
要初始化腳本,可以這樣調用它: <body onload="prettyPrint();">
可以正常工作。
我不想將此屬性添加到頁面的所有<body>
標記中,相反,我想從<body>
內的<body>
<script>
標記中調用它(出於較長時間的解釋)。 但這是行不通的。
這是代碼:
<body>
<script type="text/javascript">
prettyPrint();
//document.write('doing something');//this line prints
</script>
...
代碼片段沒有像我通過<body onload="prettyPrint();">
調用該函數時那樣突出顯示,但是JavaScript控制台上沒有錯誤,並且'doing something'
行。
如何從<script>
標記調用此函數?
編輯
即使正在打印,但語法仍未突出顯示:
<body>
<script type="text/javascript">
if(window.prettyPrint){
prettyPrint();
document.write('doing something');//this line prints
}
</script>
...