首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用unbind()和bind ()函数

使用unbind()和bind ()函数
EN

Stack Overflow用户
提问于 2014-08-22 16:34:11
回答 1查看 52关注 0票数 0

嗨,我用prev/next构建了一个图像滑块。在取消绑定prev-或next按钮后,我想再次绑定它们的问题,我如何将bind()集成到我的代码中?

这是我的代码:

代码语言:javascript
复制
// for sliding right
var counter = 1;
$('#nextArrow').click(function(){
    var item_width = $('.productList li').width(); 
    var left_indent = parseInt($('.productList').css('left')) - item_width;
    $('.productList').animate({'left' : left_indent},function(){
        if( counter > $('.li_group').length - 1) {
            $('.nextArrow').addClass('disabled');
            $('.prevArrow').removeClass('disabled');
            $('#nextArrow').unbind('click');
        }
    });
    counter++;
});

// for sliding left
$('#prevArrow').click(function(){

    var item_width = $('.productList li').width();

    var left_indent = parseInt($('.productList').css('left')) + item_width;
    $('.productList').animate({'left' : left_indent},function(){
        if( counter == 1) {
            $('.prevArrow').addClass('disabled');
            $('.nextArrow').removeClass('disabled');
            $('#prevArrow').unbind('click');
        }
    });
    counter--;
});
EN

回答 1

Stack Overflow用户

发布于 2014-08-22 16:38:49

您根本不需要使用bindunbind。向#nextArrow#prevArrow元素添加一个类,并使用 将选择器更改为只处理不包含这些类的元素。

例如,在#nextArrow单击处理程序上,可以将此类添加到#nextArrow元素中,并确保将其从#prevArrow元素中删除:

代码语言:javascript
复制
if( counter > $('.li_group').length - 1) {
    $('#nextArrow').addClass('ignore-click');
    $('#prevArrow').removeClass('ignore-click');
    ...
}

然后,您可以修改jQuery选择器,使其仅影响不具有ignore-click类的元素:

代码语言:javascript
复制
$('#nextArrow:not(.ignore-click)').click(...);

$('#prevArrow:not(.ignore-click)').click(...);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25442801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档