2012-01-20 30 views
1

我開發了一個使用.load函數動態加載頁面的Wordpress站點。jQuery在動態加載的HTML上執行JavaScript

我安裝了幾個插件,我需要工作,沒有替代品。我的問題是這些插件在加載到div#content時依賴於JavaScript(AJAX聯繫表單和照片庫),所需的JavaScript不會在動態加載的內容上執行。

一個正常的解決方案是加載我需要的JavaScript頁面,但是因爲這是通過Wordpress完成的,並且<script>標籤由插件放置在<head>。我考慮過的另一個解決方案是使用jQuery的$.getScript,但是由於JS已經處於頭頂,這將導致衝突。我真的堅持下一步要去哪裏。

任何幫助表示讚賞的傢伙。

+1

爲什麼不打電話的.load()方法的「完整」的回調插件初始化? –

+0

@DidierGhys如果你發佈這個答案我會接受它給你的代表 –

回答

1

爲什麼不使用.load()方法的「完整」回調來將插件實例化爲添加的元素?

.load(url [, data] [, complete(responseText, textStatus, XMLHttpRequest)]) 

url: A string containing the URL to which the request is sent.  
data: A map or string that is sent to the server with the request.  
complete(responseText, textStatus, XMLHttpRequest): A callback function that is executed when the request completes. 

的使用方法是:

$('#insertIntoThis').load('url', function(data, status, xhr) { 
    // instantiate here 
    // $('#myelement').myPlugin(); 
}); 
相關問題