2012-12-14 6 views
3

ajax 호출 후에 스크립트를 다시 실행할 수있는 방법이 있습니까?ajax 호출 후 jquery 함수를 다시 실행하십시오.

가 나는 photoswipe이 (라이트) JQuery와 플러그인이 같은 전화 :

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

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

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

    } 
}); 

나는 또한 아약스 '로드 게시물'기능을 가지고 있고, 분명히 photoswipe가로드 된 후 이미지를 대상으로하지 않습니다 첫 번째 페이지로드.

아약스에 대한 많은 지식이 없습니까? 감사합니다

UPDATE : 나는이 방법 functions.php 워드 프레스에 전화

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

: 다음은 '로드'더 스크립트의

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'); 
+3

어떻게 ajax 통화를하고 있습니까? –

+0

코드를 함수로 옮기고 함수를'$ (document) .ready()'및 Ajax 요청 후에 호출하십시오. – Morpheus

+0

모든 관련 코드로 질문을 편집했습니다. – djwd

답변

1

함수에 넣어, pageload에있는 전화 그리고 아약스 전화에서.

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

감사합니다. 논리를 이해합니다. 하지만 "URL :"에 대해 설정해야하는 것은 무엇입니까? – djwd

+0

내 대답이 업데이트되었습니다. $ .ajax가 아닌 $ .load를 사용하고 있습니다. – iappwebdev

+0

그래, 내가 실종됐다. 좋아, 나는 거의 다 왔어, 그것은 ajax-load-more.js에서 전체 함수를 복제 할 때만 작동한다. 그렇지 않으면 setMyPhotoSwipe()를 얻는다; 정의되지 않았습니다. (다른 js 파일에있는 첫 번째 사진 닦기 호출). 그러나이 함수를 복제하면이 오류가 발생합니다. 잡히지 않음 Code.PhotoSwipe.activateInstance : 다른 인스턴스가 이미이 대상에 대해 활성화되어 있기 때문에 활성화 할 수 없습니다. 모든 것이 제대로 작동한다고해도 나는 그다지 '우아하지 않다'고 생각하지 않는다. – djwd

0

나는 아래

jQuery(document).ready(InitializeSettings); 

는 아약스에서 같은 전화를 제안 모피어스 말했듯이

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


    function InitializeSettings(){ 

    if($('.img-frame a').length > 0){ 
     var myPhotoSwipe = $(".img-frame a").photoSwipe(); 
    } 
    } 
0

를 호출하는 것입니다.

코드를 넣는 별도의 함수를 만듭니다.

당신은 (: http://api.jquery.com/jQuery.ajax/ 설명서를 참조하십시오) 그런 다음 아약스 성공 경로에 같은 기능을 사용하여 $(document).ready()

내에서 그 함수를 호출 할 수 있습니다.

또는 당신은이를 사용할 수 있습니다 http://api.jquery.com/ajaxStop/

그것은 당신의 모든 아약스 전화가 정지 할 때 같은 기능을 사용하도록 촉진 할 것이다.

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

이것은 @ Simon의 답변이지만 콜백 기능이 하나 더 있습니다! –

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

}); 
+0

대신에 mousedown 대신 mousemove를 사용할 수 있습니까? @ roasted 나는 그것에 대해 알고 싶다! –

+0

나는이 플러그인을 모르지만 당신이 시도 할 수 있다고 확신한다 –

+0

BTW 당신의 대답은 굉장한 사람입니다! 그리고 내 편에서 +1 :) 나는 또한 시도하지 않았지만 나는 그 올바른 생각! –

0

미안 ... JQuery와의 초보자이야 어떻게()

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

ajaxComplete 사용에 대한

ajax 호출 후 함수를 실행해야만 ... ajaxComplete()가 트릭을 수행했습니다 ...

$(document).ready(function(){ 

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

    $(document).ajaxComplete(function() { 

    some_function(); 

    }); 
}) 
+0

고마워요! ** Ajax Load After Posts WP Plugin **을 설치 한 후 WP 플러그인으로 작성된 ajax 호출 후에로드되는 포스트 이미지에 jquery 플러그인 초기화 기능이 작동하지 않아 내 페이지에서이 함수를 사용했습니다. 내 사용자 정의 게시 유형 페이지 템플리트에서만 함수를로드하는 wp 상수가있는 템플리트. –

관련 문제