jquery网页刷新后控件失效_在AJAX页面更新后重新初始化jQuery插件

I have an image carousel in home page. To render it i use Jquerytools ( scrollable+navigator )

I trigger the jQuery initializer script in this way:

$(window).load(function(){

$("#today-news-carousel").scrollable({ vertical: true, mousewheel: true }).navigator({ navi: '#today-news-navigator' });

});

The content of this carousel can be updated by AJAX call.

After this call i need to reinitialize this carousel.

Here the function that makes AJAX call:

$(document).on('click', '.nav-highlight', function() {

var requestDateArray = $(this).attr('data-thedate').split('-');

var d = new Date();

var requestedDate = new Date(requestDateArray[0], (requestDateArray[1]-1), requestDateArray[2]);

var today = new Date(d.getFullYear(), d.getMonth(), d.getDate());

if (requestedDate > today) {

return

}else {

$.ajax({

type: "POST",

url: templateDir+'/highlight-news-navigator.php',

context: this,

dataType: "html",

data: { date: $(this).attr('data-thedate') },

beforeSend: function(){

},

success: function(data) {

$('.today-news').fadeOut('fast', function(){

$(this).empty().html(data).fadeIn('fast');

});

},

complete: function(){

$("#today-news-carousel").scrollable({ vertical: true, mousewheel: true })

.navigator({ navi: '#today-news-navigator' });

}

});

}

});

in the "complete" callback function i try to reinitialize the plugin but i have the following error in console:

TypeError: $(...).scrollable(...).navigator is not a function

.navigator({ navi: '#today-news-navigator' });

I cannot understand why it works correctly when i load the page and when i reinitialize it seems it cannot find .navigator method...

解决方案

Thank to the help of a Archer i found the solution.

The script to reinitialize the plugin had to be located in the fadeIn() callback.

Here the working code:

$(document).on('click', '.nav-highlight', function() {

var requestDateArray = $(this).attr('data-thedate').split('-');

var d = new Date();

var requestedDate = new Date(requestDateArray[0], (requestDateArray[1]-1), requestDateArray[2]);

var today = new Date(d.getFullYear(), d.getMonth(), d.getDate());

if (requestedDate > today) {

return

}else {

$.ajax({

type: "POST",

url: templateDir+'/highlight-news-navigator.php',

context: this,

dataType: "html",

data: { date: $(this).attr('data-thedate') },

beforeSend: function(){

},

success: function(data) {

$('.today-news').fadeOut('fast', function(){

$(this).empty().html(data).fadeIn('fast', function(){

$("#today-news-carousel").scrollable({ vertical: true, mousewheel: true }).navigator({ navi: '#today-news-navigator' });

});

});

},

});

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值