在ajax調用之後再次運行腳本有什麼方法嗎?在ajax調用後再次運行jquery函數
我有一個photoswipe(燈箱)的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');
你打算怎麼做ajax調用? –
將你的代碼移動到'$(document).ready()'中調用該函數,並在ajax請求後調用 – Morpheus
我編輯了所有相關代碼的問題 – djwd