2013-09-30 41 views
0

我想替換側腳本標記中的jQuery函數。我在</body>標記之前的佈局中有這個jQuery函數。jQuery:使用replaceWith函數

<div id="white_backstretch"> 
    <script> 
    $.backstretch("/body-bg.jpg"); 
    </script> 
</div> 

在另一頁(顯示頁)我:

<script> 
    $('#white_backstretch').replaceWith("<script>" + $.backstretch(<%= @white_label.logo %> + "</script>"); 
</script> 

在這裏,我試圖取代現有的

<script>$.backstretch("/body-bg.jpg");</script> 

<script>$.backstretch(<%= @profile.logo %></script> 

,這樣,當不同的頁面會出現背景圖片更改。但我面臨的問題是:

<script> 
    $('#white_backstretch').replaceWith("<script>" +  $.backstretch(/uploads/white/logo/4/image14.png) + "</script>"); 

未捕獲的SyntaxError:意外的標記非法

首先結束</script>標籤內替換功能被作爲腳本的結束使「);」 < - 拋出意外的令牌錯誤

因此,如果任何人告訴我如何用另一個jQuery方法替換腳本標記中的jQuery方法,它將會有幫助嗎? (這個方法也應該在腳本標籤裏面,因爲我想在html文件裏面使用replaceWith jQuery方法)

回答

1

$.replaceWith只會替換而不執行javascript。即使你能夠得到它的工作,它也是沒有用的。

我建議你這樣做的另一種方式,通過使用函數重載

<div id="white_backstretch"> 
    <script> 
     function replace() { 
      $.backstretch("/body-bg.jpg"); 
     }   
    </script> 
</div> 

而且在你的節目頁面

document.write("<script>function replace(){$.backstretch("<%= @profile.logo %>");} </script>") 

並在頁面加載調用replace()或徘徊無論它是適當的。