2011-10-04 29 views
0

我有一个页面有5张图片和一个按钮来加载更多使用AJAX。我加载使用这个脚本其余的照片:灯箱不工作在AJAX加载部分

<script type="text/javascript"> 
         $(document).ready(function(){ 
          $("#loadmorebutton").click(function(){ 
           $('#loadmorebutton').html('<img src="<?php bloginfo('template_url'); ?>/img/ajax-loader.gif" />'); 
           $.ajax({ 
            url: "<?php bloginfo('template_url'); ?>/includes/loadmore.php?lastid=" + $(".postitem:last").attr("id"), 
            success: function(html){ 
             if(html){ 
              $("#wallPosts").append(html); 
              $('#loadmorebutton').html('Load More'); 
             }else{ 
              $('#loadmorebutton').replaceWith('<center>No more posts to show.</center>'); 
             } 
            } 
           }); 
          }); 
         }); 
        </script> 

首先,显示5张图片,当用户点击旧帖子多5被加载。现在我的问题是,虽然所有的图片都相同的代码/类/结构,灯箱只适用于第5张图片,而不是通过AJAX加载的一个。有人可以帮我解决这个问题吗?

回答

1
$(document).ready(function(){ 
    $("#loadmorebutton").live('click', function(){ 
     $('#loadmorebutton').html('<img src="<?php bloginfo('template_url'); ?>/img/ajax-loader.gif" />'); 
     $.ajax({ 
      url: "<?php bloginfo('template_url'); ?>/includes/loadmore.php?lastid=" + $(".postitem:last").attr("id"), 
      success: function(html){ 
       if(html){ 
        $("#wallPosts").append(html); 
        $('#loadmorebutton').html('Load More'); 
       }else{ 
        $('#loadmorebutton').replaceWith('<center>No more posts to show.</center>'); 
       } 
      } 
     }); 
    }); 
}); 

使用jQuery.Live()事件附加到将通过AJAX添加到页面对象。否则,事件仅在调用该函数时绑定到页面上的元素。文档可以发现here

+0

你也可以帮我转换这一块? '$(document).ready(function(){$('#commentBox <?php echo $ row [' id'];?>')。hide(); $('#comment <? php echo $'[' '];?>')。show }); });” – Andrei

+0

你试图让每一个评论框隐藏,因为它们是加载还是你通过CSS这样做呢?我会默认的CSS显示:无,然后你不需要隐藏线,只需要在你的document.ready()中使用以下内容:$('#comment <?php echo $ row ['id']; ();')。'live('click',function(){$('#commentBox <?php echo $ row ['id'];?>')。show(); });' – PCasagrande

+0

如果解决了你的问题请标记为答案;否则问题将继续显示为未答复。 – PCasagrande