Phi*_*olb 6 ajax wordpress jquery pagination
我正在使用下面的循环+ jQuery加载下一组页面,它在第一次单击时工作,但是当下载页面加载并点击"较新的帖子"时,它会重新加载整个页面.有任何想法吗?
<div id="content">
<?php
$new_query = new WP_Query();
$new_query->query('post_type=post&showposts=1'.'&paged='.$paged);
?>
<?php while ($new_query->have_posts()) : $new_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<div id="pagination">
<?php next_posts_link('« Older Entries', $new_query->max_num_pages) ?>
<?php previous_posts_link('Newer Entries »') ?>
</div>
</div><!-- #content -->
<script>
$('#pagination a').on('click', function(event){
event.preventDefault();
var link = $(this).attr('href'); //Get the href attribute
$('#content').fadeOut(500, function(){ });//fade out the content area
$('#content').load(link + ' #content', function() { });
$('#content').fadeIn(500, function(){ });//fade in the content area
});
</script>
Run Code Online (Sandbox Code Playgroud)
ade*_*neo 23
您正在使用jQuery的load()方法插入内容,这是一种快捷方式$.ajax,当然也会动态插入内容.
动态内容需要将事件委托给非动态父级,jQuery可以轻松实现 on()
jQuery(function($) {
$('#content').on('click', '#pagination a', function(e){
e.preventDefault();
var link = $(this).attr('href');
$('#content').fadeOut(500, function(){
$(this).load(link + ' #content', function() {
$(this).fadeIn(500);
});
});
});
});
Run Code Online (Sandbox Code Playgroud)