activate lightbox on dynamically added content

﹥>﹥吖頭↗ 提交于 2019-11-29 16:29:49

If you are binding the links with a regular event handler:

$('a[rel=lightbox]').click(function(e){ ... });

Then any new content that is added after the bind won't be captured.

You can either rerun the bind when you change out the content, or more simply use jQuery's event delegation to attach the handler:

$('a[rel=lightbox]').live('click', function(e){ ... });

You can call many jQuery lightboxes with $.lightboxName(), e.g.

$('a[rel=lightbox]').live('click', function(e){
  e.preventDefault;
  $.fancybox({ href : $(this).attr('href') });
  // OR $.colorbox({ href : $(this).attr('href') });
});

Note that jQuery now recommends using .on() instead of .live().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!