Appending dynamically generated html using jQuery does not play well with Fancybox

前端 未结 3 1134
醉话见心
醉话见心 2020-12-21 02:50

I\'m using Fancybox (http://fancybox.net) to pop open an iFrame. In order to do so, I create a link with a class set to \"item_pop\" as so:

3条回答
  •  感情败类
    2020-12-21 03:18

    what's happening is that jQuery(".item_pop") finds elements matching the selector at that time and binds to those. Since you're creating new elements later, you need to bindto them.

    After this call:

    jQuery('#events').append(html);
    

    You need to run the .fancybox() call again, on this set of new elements:

    jQuery("#events .item_pop").fancybox({ ... });
    

    Or alternatively (and less efficient) you can use the .livequery() plugin, like this:

    jQuery(".item_pop").livequery(function() {
      jQuery(this).fancybox({
        'width'             : 900,
        'height'            : 500,
        'autoDimensions'    : true,
        'autoScale'         : true,
        'transitionIn'      : 'elastic',
        'titleShow'         : false,
        'transitionOut'     : 'elastic',
        'centerOnScroll'    : true,
        'type'              : 'iframe'
      });
    });
    

提交回复
热议问题