Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在ajax调用之后再次运行jquery函数_Javascript_Jquery_Ajax_Run Script - Fatal编程技术网

Javascript 在ajax调用之后再次运行jquery函数

Javascript 在ajax调用之后再次运行jquery函数,javascript,jquery,ajax,run-script,Javascript,Jquery,Ajax,Run Script,有没有办法在ajax调用后再次运行脚本 我有一个Photoswip(lightbox)jquery插件,我这样称呼它: jQuery(document).ready(function($){ if( $('.img-frame a').length > 0 ){ var myPhotoSwipe = $(".img-frame a").photoSwipe(); } }); 我还有一个ajax“加载更多帖子”功能,显然photosweep不会以加载

有没有办法在ajax调用后再次运行脚本

我有一个Photoswip(lightbox)jquery插件,我这样称呼它:

jQuery(document).ready(function($){

    if( $('.img-frame a').length > 0 ){

        var myPhotoSwipe = $(".img-frame a").photoSwipe();

     }
});
我还有一个ajax“加载更多帖子”功能,显然photosweep不会以加载第一页后加载的图像为目标

我没有太多的ajax知识,在这方面有什么帮助吗?谢谢

更新:下面是“加载更多”脚本:

jQuery(document).ready(function($) {

    // The number of the next page to load (/page/x/).
    var pageNum = parseInt(djwd_load_posts.startPage) + 1;

    // The maximum number of pages the current query can return.
    var max = parseInt(djwd_load_posts.maxPages);

    // The link of the next page of posts.
    var nextLink = djwd_load_posts.nextLink;

    /**
     * Replace the traditional navigation with our own,
     * but only if there is at least one page of new posts to load.
     */
    if(pageNum <= max) {
        // Insert the "More Posts" link.
        $('#content')
            .append('<div class="lp-placeholder-'+ pageNum +'"></div>')
            .append('<p id="lp-load-posts" class="long-button"><a href="#">Load More Posts<i class="icon-chevron-down icon-large"></i></a></p>');

        // Remove the traditional navigation.
        $('#nav-below').remove();
    }


    /**
     * Load new posts when the link is clicked.
     */
    $('#lp-load-posts a').click(function() {

        // Are there more posts to load?
        if(pageNum <= max) {

            // Show that we're working.
            $(this).text('Loading posts...');

            $('.lp-placeholder-'+ pageNum).load(nextLink + ' .post',
                function() {

                    $( this ).hide().fadeIn(700);

                    // Update page number and nextLink.
                    pageNum++;
                    nextLink = nextLink.replace(/\/page\/[0-9]?/, '/page/'+ pageNum);

                    // Add a new placeholder, for when user clicks again.
                    $('#lp-load-posts')
                        .before('<div class="lp-placeholder-'+ pageNum +'"></div>')

                    // Update the button message.
                    if(pageNum <= max) {
                        $('#lp-load-posts a').text('Load More Posts');
                    } else {
                        $('#lp-load-posts a').text('No more posts to load.');
                    }
                }
            );
        } else {
            $('#lp-load-posts a').append('.');
        }   

        return false;
    });
});

将其放入函数中,在pageload和ajax调用中调用

function setMyPhotoSwipe() {
    var $targets = $('.img-frame a').not('.photo-swipe');

    if($targets.length > 0 ){
        $targets.addClass('photo-swipe').photoSwipe();
    };
};

jQuery(document).ready(function($){
    setMyPhotoSwipe();
});
顺便说一下,如果不需要变量myphotosweep,那么就不必设置它。您还使用了两次
$('.img frame a')
,因此缓存结果

和你的负载呼叫:

$('.lp-placeholder-'+ pageNum).load(nextLink + ' .post',
            function() {

                $( this ).hide().fadeIn(700);

                // Update page number and nextLink.
                pageNum++;
                nextLink = nextLink.replace(/\/page\/[0-9]?/, '/page/'+ pageNum);

                // Add a new placeholder, for when user clicks again.
                $('#lp-load-posts')
                    .before('<div class="lp-placeholder-'+ pageNum +'"></div>')

                // Update the button message.
                if(pageNum <= max) {
                    $('#lp-load-posts a').text('Load More Posts');
                } else {
                    $('#lp-load-posts a').text('No more posts to load.');
                }

                // New content has been loaded and insertet, so set up photo swipe
                setMyPhotoSwipe();
            }
        );
$('.lp占位符-'+pageNum).load(nextLink+'.post',
函数(){
$(this.hide().fadeIn(700);
//更新页码和下一页链接。
pageNum++;
nextLink=nextLink.replace(/\/page\/[0-9]?/,'/page/'+pageNum);
//添加新的占位符,以便用户再次单击时使用。
$(“#低压负载柱”)
.之前(“”)
//更新按钮消息。

如果(pageNum我建议如下

 jQuery(document).ready(InitializeSettings);
在ajax调用中也调用相同的函数

  ajax_success_function(){ // depends on your ajax call
   InitializeSettings();
  }


  function InitializeSettings(){

    if( $('.img-frame a').length > 0 ){
       var myPhotoSwipe = $(".img-frame a").photoSwipe();
     }
  }
就像莫菲斯说的

创建一个单独的函数,将代码放入其中

您可以在
$(document).ready()中调用该函数

然后在ajax成功路径中使用相同的函数(检查文档:)

或者,您可以使用以下选项:


当每次Ajax调用停止时,它将方便您使用相同的功能。

插件没有授权,由作者的插件合并。为什么不作弊:(对不起,无法测试代码,也不确定它是否与Photoswip插件相关)

$.ajax({
    url: 'url/test.php',
    success: function(data) { // also gets response from php and can be manipulated if required!
        setMyPhotoSwipe();
    }
});

抱歉,我是jquery的新手……使用ajaxComplete()怎么样

有一次我不得不在ajax调用后执行一个函数…ajaxComplete()完成了这项任务

$(document).ready(function(){

  function some_function()
  {
  //---- this function to be called after ajax request...
  }

  $( document ).ajaxComplete(function() {

    some_function();

  });
})

如何进行ajax调用?将代码移动到函数并在
$(document.ready()中调用该函数
在ajax请求之后,我用所有相关的代码编辑了这个问题这是@Simon的答案,但还有一个回调功能!嘿,我们不能用mousedown而不是mousedown吗?@我想知道它!我不知道这个插件,但我相信你可以试试看你的答案是不是很棒!还有我这边的+1:)我也没有试过,但我认为它是正确的!好的,谢谢,我理解逻辑。但是我必须为“URL:”设置什么?是的,那是我丢失的。好的,我就快到了,只有在我复制ajax-load-more.js中的完整函数时它才有效,否则我得到setMyPhotosweep();没有定义。(第一次Photosweep调用它在另一个js文件中)。但如果复制该函数,则会出现以下错误:未捕获代码。Photosweep.activateInstance:无法激活实例,因为另一个实例对此目标已处于活动状态。尽管一切正常,但我认为它不是很“优雅”(抱歉,我试图设置更好的注释格式,但我用光了5分钟)那么你必须标记那些之前已经加载的链接,我更新了我的答案,请参见函数
setphotoswip()
。关于你的错误:将函数定义放在
jQuery(document)之外。准备好了吗?
。我只是这样设置函数:$.fn.setMyPs=function()…然后像你在我的ajax调用中说的那样运行它,现在一切都完美无瑕。非常感谢@Simon和所有人。谢谢!在安装ajax Load More Posts WP插件后,放大弹出jquery Plugin init函数对于在WP插件发出ajax调用后加载的帖子图像不起作用,因此,我在我的页面模板上使用了这个带有wp常量的函数,以便仅在我的自定义帖子类型页面模板上加载该函数,并且它工作正常。
jQuery(document).ready(function($){

    $(this).on('mousedown','.img-frame a',function(){ //document or better parent container// mousedown or any relevent event use by photoSwipe
          if(!$(this).data('swiped')) {
              $(this).data('swiped',true).photoSwipe();
          }
    });

});
$(document).ready(function(){

  function some_function()
  {
  //---- this function to be called after ajax request...
  }

  $( document ).ajaxComplete(function() {

    some_function();

  });
})