2013-04-05 135 views
3

example沒有顯示確切的問題,只是有一個想法。例如,當我點擊頁面2時,內容不會加載到div中,我必須刷新頁面才能看到頁面2的內容。使用jQuery mobile無法動態加載到DIV中的內容

P.S:相同的代碼在其他頁面中重複。

代碼:

$(document).ready(function() { 
    $('.content-primary').html("Content of page 1"); // This line just in this example 
    //$(".content-primary").load("content-of-page.php"); // but I used this code on all my pages 
}); 
+0

你意思是這樣的http://jsfiddle.net/gCsDz/6/ – Morpheus 2013-04-05 14:45:14

+0

哦對不起,當我更新JSFIDDLE,我得到這個錯誤。 – Mils 2013-04-05 14:47:51

回答

3

這是一個常見的jQuery Mobile的問題。

您不應該使用jQM準備好文檔,而應該使用jQM提供的頁面事件。您的問題是文檔已準備好誰可以,並且通常在頁面加載到DOM之前觸發。這就是爲什麼刷新有幫助,在這一點上已經加載頁面。

我給你做一個工作示例:http://jsfiddle.net/Gajotres/8hKe2/

$(document).on('pagebeforeshow', '#foo', function(){  
    alert('This is foo'); 
}); 

$(document).on('pagebeforeshow', '#bar', function(){  
    alert('This is bar'); 
}); 

基本上每個頁面事件將觸發JavaScript代碼只是針對該網頁。如果你希望你的代碼只執行一次,pageinit事件應該被用來代替pagebeforeshow,像這樣:

$(document).on('pageinit', '#foo', function(){  
    alert('This is foo'); 
}); 

$(document).on('pageinit', '#bar', function(){  
    alert('This is bar'); 
}); 

如果您想了解更多的看看我的另一篇文章/回答:https://stackoverflow.com/a/14469041/1848600