2013-03-19 66 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>');