dongyong1942 2013-03-01 18:07
浏览 51
已采纳

无法在DOM中导航动态创建的页面

After so many trials, I have finally managed to create pages dynamically using PHP, JSON and AJAX and load them into DOM. But the problem now is I'm unable to call/navigate those pages dynamically, but manually i.e gallery.html#page1 ...etc.

I seek guidance rather than burdening you, as I'm here to learn.

**PHP - photos.php **

$photos = array();
$i=0;
while ($row = mysqli_fetch_array($query)){
 $img = $row["fn"];
 $photos[] = $img;
 $i++;
}

$count = count($photos);
echo json_encode(array('status' => 'success', 'count' => $count, 'items' => $photos));

JSON array

{
"status":"success",
"count":3,
"items":
 [
"img1.jpg",
"img2.jpg",
"img3.jpg"
 ]
}

I use the below method to fetch and store ID of the desired gallery,

<input type="hidden" value="<?php echo $id; ?>" id="displayid" />

and then I call it back to use it in AJAX.

var ID = $('#displayid').val();

AJAX and JQM

$.ajax({
Type: "GET",
url: 'photos.php',
data: { display: ID }, // = $('#displayid').val();
dataType: "json",
contentType: "application/json",
success: function(data) {
 var count = data.count;
 var number = 0;
 $.each(data.items, function(i,item) {
  var newPage = $("<div data-role=page data-url=page" + number + "><div data-role=header><h1>Photo " + number + "</h1></div><div data-role=content><img src=" + item + " /></div></div");
  newPage.appendTo( $.mobile.pageContainer );
  number++;
  if (number == count) { $.mobile.changePage( newPage ); }; // it goes to last page

I got this code from here thanks Gajotres to dynamically navigate between pages. It's within the same code.

$(document).on('pagebeforeshow', '[data-role="page"]', function(){       
 var nextpage = $(this).next('div[data-role="page"]');
 if (nextpage.length > 0) {    
  $.mobile.activePage.find('[data-role="header"]').append($('<a>').attr({'href':'#'+nextpage.attr('id'),'data-theme':'b'}).addClass('ui-btn-right').html('Next').button());
 }  
}); // next button 
}); // each loop 
} // success
}); //ajax
  • 写回答

1条回答 默认 最新

  • dpdt79577 2013-03-01 19:07
    关注

    I found your problem.

    This part of code can't be used here like this:

    $(document).on('pagebeforeshow', '[data-role="page"]', function(){       
        var nextpage = $(this).next('div[data-role="page"]');
        if (nextpage.length > 0) {    
            $.mobile.activePage.find('[data-role="header"]').append($('<a>').attr({'href':'#'+nextpage.attr('id'),'data-theme':'b'}).addClass('ui-btn-right').html('Next').button());
        }  
    });
    

    This is the problem. First remove pagebeforeshow event binding, it can't be used here like that. Rest of the code is not going to do anything because currently there are any next page (next page is going to be generated during then next loop iteration), so remove this whole block.

    Now, after the each block ends and all pages are generated (that is the main thing, all pages should exist at this point), add this code:

    $('[data-role="page"]').each(function(){
        var nextpage = $(this).next('div[data-role="page"]');
        if (nextpage.length > 0) {    
            $(this).find('[data-role="header"]').append($('<a>').attr({'href':'#'+nextpage.attr('id'),'data-theme':'a'}).addClass('ui-btn-right').html('Next').button());
        }         
    });
    

    This is what will happen. Each loop will loop through every available page (we have them all by now) and in case it is not the last one it will add next button.

    Here's a live example: http://jsfiddle.net/Gajotres/Xjkvq/

    Ok in this example pages are already there, but point is the same. They need to exist (no matter if you add them dynamically or if they are preexisting) before you can add next buttons.

    I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘