重新绑定点击事件

时间:2012-11-05 12:28:09

标签: jquery bind unbind

我有一个与此类似的问题:Rebind但我不明白解决方案。

我有一个左右滑动html内容的旋转木马......左右图像用于滑动内容。如果到达轮播的末尾,则右滑动图像上的单击事件应该是未绑定的。如果单击左侧图像,则右侧图像的单击事件应再次限制...如下所示重新绑定它似乎不起作用。似乎我应该存储对click事件的引用,但我无法做到正确。

$(document).ready(function() {  

         //when user clicks the image for sliding right  
        $('#right_scroll img').click(function(){  

            // code for sliding content to the right, unless end is reached

                if($('#carousel_ul li:first').attr('id') == fifth_lli){ // end carousel is reached

                $('#right_scroll img').removeAttr('onmouseover');
                $('#right_scroll img').removeAttr('onmouseout');
                $('#right_scroll img').removeAttr('onmousedown');
                $('#right_scroll img').unbind('click');
                $('#right_scroll img').attr("src", "Images/gray_next_1.png");   

                };
            });  

        //when user clicks the image for sliding left  
        $('#left_scroll img').click(function(){  

            //if at end of carousel and sliding back to left, enable click event for sliding on the right...
            if($('#carousel_ul li:first').attr('id') == fifth_lli){

                $('#right_scroll img').attr("src", "Images/red_next_1.png");
                $('#right_scroll img').bind('click');   // this doesn't work.

            };      
        });  

  }); 

1 个答案:

答案 0 :(得分:0)

而不是取消绑定和重新绑定,只需检查轮播是否在结束或开始移动之前。

$(document).ready(function() {
    $('#right_scroll img').click(function() {
        if ($('#carousel_ul li:first').attr('id') !== fifth_lli) {
            //slide carousel right
        }
    });
    $('#left_scroll img').click(function() {
        if ($('#carousel_ul li:first').attr('id') !== first_lli) { //changed it to first first_lli, i figure that would be the end of the left scrolling
            //slide carousel left
        }
    });
});