4

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

我有一个 photoswipe (lightbox) jquery 插件,我这样调用:

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

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

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

     }
});

我还有一个 ajax '加载更多帖子' 功能,显然 photoswipe 不针对在第一页加载后加载的图像。

我没有太多的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;
    });
});

我在 Wordpress functions.php 中这样称呼它:

 function djwd_ajax_load_init() {
    global $wp_query;

    if( !is_singular() ) {
        wp_enqueue_script('ajax-load-posts', get_template_directory_uri() . '/js/ajax-load-posts.js', array('jquery'), true );

        $max = $wp_query->max_num_pages;
        $paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;

        wp_localize_script(
            'ajax-load-posts',
            'djwd_load_posts',
            array(
                'startPage' => $paged,
                'maxPages' => $max,
                'nextLink' => next_posts($max, false)
            )
        );
    }
 }
 add_action('template_redirect', 'djwd_ajax_load_init');
4

6 回答 6

2

插件没有委托,由作者的插件来合并它。为什么不作弊:(抱歉,无法测试代码并且不确定它是否与 photoSwipe 插件相关)

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();
          }
    });

});
于 2012-12-14T13:00:15.893 回答
2

把它放在一个函数中,在页面加载和你的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();
});

顺便说一句,如果不需要变量 myPhotoSwipe,那么您不必设置它。你也使用$('.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();
            }
        );
于 2012-12-14T12:54:28.293 回答
1
$.ajax({
    url: 'url/test.php',
    success: function(data) { // also gets response from php and can be manipulated if required!
        setMyPhotoSwipe();
    }
});
于 2012-12-14T12:56:52.543 回答
0

对不起,我是 jquery 的新手......如何使用 ajaxComplete()

http://api.jquery.com/ajaxcomplete/

一旦我不得不在 ajax 调用之后执行一个函数......并且 ajaxComplete() 成功了......

$(document).ready(function(){

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

  $( document ).ajaxComplete(function() {

    some_function();

  });
})
于 2014-06-03T07:49:30.710 回答
0

我建议以下

 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();
     }
  }
于 2012-12-14T12:55:09.103 回答
0

正如墨菲斯所说。

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

您可以在您的内部调用该函数$(document).ready()

然后在您的 ajax 成功路径中使用相同的函数(查看文档:http ://api.jquery.com/jQuery.ajax/ )。

或者你可以使用这个:http ://api.jquery.com/ajaxStop/

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

于 2012-12-14T12:55:14.890 回答