jQuery Masonry and Ajax Append Items?

前端 未结 13 1391
借酒劲吻你
借酒劲吻你 2020-12-08 07:19

I am trying to use some ajax and the jQuery Masonry plugin to add some items - but for some reason the new items aren\'t getting the masonry applied ?

I\'m using

13条回答
  •  一生所求
    2020-12-08 08:02

    it is clearly explained here https://masonry.desandro.com/methods.html#prepended

    jQuery.ajax({
        type: "POST",
        url: ajax_url,
        data: ajax_data,
        cache: false,
        success: function (html) {
            if (html.length > 0) {
                jQuery("#content").append(html).masonry( 'appended', html, true );
            }
        });
    });
    

    in your success function, you need your response "html" to be wrapped in a jquery object and then append using html() or append().

    var $content = $( html );
    jQuery("#content").append($content).masonry( 'appended', $content );
    

    the final code should be

    jQuery.ajax({
        type: "POST",
        url: ajax_url,
        data: ajax_data,
        cache: false,
        success: function (html) {
            if (html.length > 0) {
                var $content = $( html );
                jQuery("#content").append($content).masonry( 'appended', $content );
            }
        });
    });
    

提交回复
热议问题