簡體   English   中英

美化prettyPrint()函數未加載到內部<script> tag

[英]Prettify prettyPrint() function not loading inside <script> tag

我正在使用自己托管的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>
    ...

因此,必須使用object.onload來調用它,我真的不知道為什么我只是嘗試了很多事情而這行得通。 實際上,無論如何,這很有意義。

    <body>
        <script>
            window.onload=function(){prettyPrint();};
        </script>
        ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM