網站搜尋

javascript - 嵌入 jquery.prettyPhoto 導致腳本在頁面上顯示


當我移動時:

<script src="js/jquery.prettyPhoto.js"></script>

收件人:

<script type="text/javascript">
   ...
</script>

然後從 粗體 部分開始,腳本就會顯示在頁面上:

style="border:none; overflow:hidden; width:500px; height:23px;" allowTransparency="true">'},s);var o=this,u=false,a,f,l,c,h,p,d=e(window).height(),v=e...

這個劇本附有以下資訊:

Class: prettyPhoto
Use: Lightbox clone for jQuery
Author: Stephane Caron (http://www.no-margin-for-errors.com)
Version: 3.1.5

我該如何將腳本正確地從 .js 檔案移到頁面上?

答案#1

當 javascript 的內容包含結束標籤 </script> 時,可能會導致瀏覽器的解析器錯誤地解析該腳本區塊的代碼。

此處的程式碼:

<script type="text/javascript">
    alert("</script>");
</script>

將無法運作,您在瀏覽器中看到的輸出將會是

");

就好像瀏覽器將腳本區塊讀作

**script start tag ->** <script type="text/javascript">
    alert("</script> **<- script end tag**

解決方案

在腳本區塊中包含 <script>...</script> 字串有兩種已知的解決方案:

  1. 將字串分成兩個獨立的字串,並使用 + 進行串接

<script type="text/javascript">
    alert("<scr"+"ipt>...</scr"+ "ipt>");
</script>
  1. 用註解區塊包住整個腳本程式碼區塊:

    <script type="text/javascript">
        <!--
        alert("<script></script>");
        -->
    </script>

請注意,如果你只有結束標籤,第二個例子將無法運作,因為瀏覽器會將該結束字串標記為原本應該關閉的開啟 <scrip> 標籤。

這個範例無法運作:

    <script type="text/javascript">
        <!--
        alert("</script>");
        -->
    </script>

特別針對你的程式碼——這是修正它的 jsfiddle。

答案#2

沒有錯誤。https://jsfiddle.net/rL9h958e/1/

這個腳本有大量== 問題以及糟糕的 HTML。

Social_Tools 在所有情況下看起來都可疑,

social_tools:'<div class="twitter"><a href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a></div>' + 
            '<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></div>' +
                     '<div class="facebook">