Help understanding jQuery button enable/disable code

后端 未结 7 854
孤独总比滥情好
孤独总比滥情好 2020-12-19 19:21

I grabbed this code form JCarousel and just trying to understand these lines below. I\'m new to jQuery and not that great at JavaScript so I am not sure what is jQuery and

相关标签:
7条回答
  • The conditional operation

    n ? 'bind' : 'unbind'
    

    gets you either the string 'bind' or 'unbind', passing that string to the [] operator gets you either the jQuery bind or unbind method. Following that result with the () invokes the method. In effect, that first part is like:

    if (n) {
        this.buttonNext.bind(this.options.buttonNextEvent, this.funcNext);
    }
    else {
        this.buttonNext.unbind(this.options.buttonNextEvent, this.funcNext);
    }
    if (p) {
        this.buttonPrev.bind(this.options.buttonPrevEvent, this.funcPrev);
    }
    else {
        this.buttonPrev.unbind(this.options.buttonPrevEvent, this.funcPrev);
    }
    

    Both the bind and unbind method return the jQuery set on which they were invoked. In this case, they will return this.buttonNext and this.buttonPrev, respectively. Following that with yet another [] operator and passing that operator the string 'removeClass' or 'addClass' gets you the removeClass or addClass jQuery method. In effect, you now have this:

    if (n) {
        this.buttonNext.bind(this.options.buttonNextEvent, this.funcNext);
        this.buttonNext.removeClass(this.className('jcarousel-next-disabled')).attr('disabled', n ? false : true);
    }
    else {
        this.buttonNext.unbind(this.options.buttonNextEvent, this.funcNext);
        this.buttonNext.addClass(this.className('jcarousel-next-disabled')).attr('disabled', n ? false : true);
    }
    if (p) {
        this.buttonPrev.bind(this.options.buttonPrevEvent, this.funcPrev);
        this.buttonPrev.removeClass(this.className('jcarousel-prev-disabled')).attr('disabled', p ? false : true);
    }
    else {
        this.buttonPrev.unbind(this.options.buttonPrevEvent, this.funcPrev);
        this.buttonPrev.addClass(this.className('jcarousel-prev-disabled')).attr('disabled', p ? false : true);
    }
    
    0 讨论(0)
提交回复
热议问题