为什么不行呢?

来源:5-8 首页.UI组件-UiSlider(3)

weixin_慕沐941136

2019-09-05 22:19:25

为什么jq里wrap.triggerHandler('auto_move');在wrap.on的前面就不行呢?


wrap

.on('move_prev',function(){

if(current<=0){

current=size;

}

current=current-1;

wrap.triggerHandler('move_to',current);

})

.on('move_next',function(){

if(current>=size-1){

current=-1;

}

current=current+1;

wrap.triggerHandler('move_to',current);

})

.on('move_to',function(evt,index){

wrap.css('left',index*width*-1);

tips.removeClass('item_focus').eq(index).addClass('item_focus');

})

.triggerHandler('auto_move');

.on('auto_move',function(){

setInterval(function(){

enableAuto&&wrap.triggerHandler('move_next');

},2000);

})


写回答

1回答

好帮手慕星星

2019-09-06

同学你好,

是triggerHandler() 方法的问题,方法的返回的是事件处理函数的返回值,而不是具有可链性的 jQuery 对象。而on绑定事件之所以可以链式调用,是因为on返回当前jQuery对象本身,这样才可以继续绑定。

如果将wrap.triggerHandler('auto_move');放在wrap.on的前面,返回值不是jQuery对象,就无法绑定on方法。

自己可以测试理解下,祝学习愉快!

0

0 学习 · 14456 问题

查看课程