jQuery .load() html content and execute script

丶灬走出姿态 提交于 2019-12-02 19:25:50

问题


I have the following functions in my document

jQuery(document).ready(function() {
jQuery('body').on('click', 'a.menu', function () { 
        var target = jQuery(this).attr('href');
    jQuery('#container').load(target+' #content');
        return false;
    });

$("a.group").fancybox();

});

The first one replaces the container with the href of the menu link. The corresponding subpages are actually plain divs but could be complete html (if necessary).

The second one calls a fancybox gallery on the selected elements.

How do i get this $("a.group").fancybox() to be executed, when the subpages are loaded?

Many thx in advance, mart05


回答1:


Load has a callback function. so execute the fancybox in this:

jQuery('#container').load(target+' #content', function() {
              $("a.group").fancybox();
          });



回答2:


You can use the complete callback feature in .loads.

jQuery('#container').load(target+' #content', function(){
    // this will run when .load has completed loading content
    $("a.group").fancybox();
});


来源:https://stackoverflow.com/questions/18758901/jquery-load-html-content-and-execute-script

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