简体   繁体   中英

Applying Shadowbox.js on dynamically generated content

I m using shadowbox.js for image gallery display.

for this we have to generate html like this

<a href="images/Ganesha3602.jpg" rel="shadowbox[my]"><img src="images/thumb.jpg"   alt="First" title="First image" /></a>
<a href="images/original.jpg" rel="shadow[my]"><img src="images/thumb.jpg" alt="Second" title="Second image" /></a>
<a href="images/original.jpg" rel="shadowbox[my]"><img src="images/thumb.jpg" alt="First" title="First image" /></a>

and then apply Shadowbox.init() on body load. Its working like a charm when the above html is static. But when i generate it dynamically using $.ajax and call Shadowbox.init() on success; shadowbox does not work. I also tried calling Shadowbox.init() on complete.

My code is something like this:

$(document).on('click','#load_posts_comments',function(){

    $.ajax({
        url:"post_comment_json.php",
        type: "POST",
        dataType: "json",
        data:{user_id:"123"},
        success: function(data){
            load_posts_comments(data);
        },
        complete: function(){
            Shadowbox.init();
        }
    });
});

Where am I going wrong? I looked the shadowbox website but there was no documentation regarding dynamically loaded content. Help would be appreciated.

You need to reinitialize it by using Shadowbox.clearCache(); and Shadowbox.setup(); .
It's been explained much better here: shadowbox stops working after jquery function call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM