【问题标题】:Empty container before .appendTo() is fired using jquery使用 jquery 触发 .appendTo() 之前的空容器
【发布时间】:2012-10-17 14:13:15
【问题描述】:

我有一个容器#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");
        });
    });

});

【问题讨论】:

    标签: jquery appendto


    【解决方案1】:

    我认为您的问题在于imagesloaded 函数。您目前将结果从imagesloaded 附加到boxes,我猜您首先将响应添加到boxes,然后在boxes 上运行imagesLoaded

    $("#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');
      });
    

    【讨论】:

    • 当我这样做时,内容会出现然后消失。
    • 好的,想通了。在imagesLoaded 中,我们只需要应用.show().masonry('reload') 就可以了。如果您同意更新您的答案,我会接受。
    • 我使用.masonry('reload') 编辑了您和我的原始代码,以便它们匹配。
    猜你喜欢
    • 2014-01-30
    • 1970-01-01
    • 2014-05-22
    • 2017-08-28
    • 2012-05-16
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多