使用jquery触发.appendTo()之前的空容器

时间:2012-10-16 13:39:49

标签: jquery appendto

我有一个容器#boxes我需要清空并添加内容以使用.imagesLoaded()然后应用砌体。默认情况下,内容包含我正在删除的砌体项目,然后添加响应中包含的新项目。我的代码存在的问题是,在我相信追加后,它会清空内容。有什么想法吗?

$(document).ready(function($){
    $('div.profile-archive').click(function(){
        $("#boxes").empty();
        $this = $(this);
        var postType = $this.attr("rel");
        data = {
          action: 'fetch_profile_archive',
              postType : postType
        };
        $.post(ajaxurl, data, function (response) {
            var $response = $(response);
            $response.imagesLoaded(function() {
              $("#boxes").masonry('reload');
            }).appendTo("#boxes");
        });
    });

});

1 个答案:

答案 0 :(得分:0)

我认为你的问题在于imagesloaded功能。您目前将imagesloaded的结果附加到boxes,我猜您首先将回复添加到boxes,然后在imagesLoaded上运行boxes

$("#boxes")
  .append($response)
  .hide() // If you don't want to show the content until loaded
  .imagesLoaded(function() {
    $("#boxes")
      .show() // If you want to show the content after it's loaded
      .masonry('reload');
  });