2013-03-19 61 views
3

我有一個應該在Content div中添加一些元素的腳本,但它不起作用。 正如你所看到的「messageBox」的內容是從一個php文件中選擇mysql表和它的數據。jquery mobile沒有將div附加到內容

這裏是JS文件的內容:

function getWallMsg(){ 
    $.getJSON('SELECT_uzenofal.php?callback=?',function(data) { 
     data.forEach(function(e){ 
      $('#posts').append($('<div class="messageBox">'+'<img src="img/profilok/'+e.kep+'"/>'+'<p class="name">'+e.nev+'</p>'+'<p>'+e.uzenet+'</p>'+'<p class="time">'+e.ido+'</p>'+'</div>')); 
     }); 
    }); 
} 

$(document).ready(function(){ 
    drawNavbar(); 
    getWallMsg(); 
}); 

和HTML:

<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" > 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" /> 
    <link rel="stylesheet" type="text/css" href="css/main.css"> 
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 
    <script type="text/javascript" src="js/main.js"></script> 
    <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script> 
</head> 
<body> 
    <!-- Home --> 
    <div data-role="page" id="fal"> 
     <!-- header --> 
     <div data-role="header"> 
      <h3> 
       Üzenőfal 
      </h3> 
     </div> 
     <!-- content --> 
     <div data-role="content" id="posts"> 

     </div> 
     <!-- footer --> 
     <div class="nav" data-role="footer" data-position="fixed" data-theme="a"></div> 
    </div> 
</body> 

我該怎麼辦?

回答

1

這應改爲:

$(document).ready(function(){ 
    drawNavbar(); 
    getWallMsg(); 
}); 

這樣:

$(document).on('pagebeforeshow', '#fal', function(){  
    drawNavbar(); 
    getWallMsg(); 
}); 

基本上document ready不應與jQuery Mobile,可以用來了解更多有關這個看看這個ARTICLE,要透明,這是我的個人博客。或找到它HERE

基本上你正在試圖將其追加對document ready當頁面內容不加載到DOM。應該使用jQuery Mobile頁面事件,如pagebeforeshow

編輯:

你甚至被添加了不正確的ID給pagebeforeshow。它現在應該工作。

+0

遺憾的是它並沒有改變:( – 2013-03-19 18:06:26

+0

如果不是你一個問題,寄給我你的代碼,我會看一看。 – Gajotres 2013-03-19 18:08:03

+0

這是不是問題,請稍等片刻:) – 2013-03-19 18:08:26

0

無法評論,因此張貼爲答案。

$('#posts').append($('<div class="messageBox">'+'<img src="img/profilok/'+e.kep+'"/>'+'<p class="name">'+e.nev+'</p>'+'<p>'+e.uzenet+'</p>'+'<p class="time">'+e.ido+'</p>'+'</div>')); 

你試過上述更改爲

$('#posts').append('<div class="messageBox">'+'<img src="img/profilok/'+e.kep+'"/>'+'<p class="name">'+e.nev+'</p>'+'<p>'+e.uzenet+'</p>'+'<p class="time">'+e.ido+'</p>'+'</div>');