2012-11-05 59 views
1

我有一个问题非常类似于这个:Rebind但我不明白的解决方案。重新绑定单击事件

我有一个左右滑动html内容的旋转木马...用于滑动内容的左右图像。如果到达转盘末端,则右侧滑动图像上的单击事件应解除绑定。如果点击左侧图像,右侧图像的点击事件应该再次被限制......像下面这样重新绑定似乎不起作用。看起来我应该存储一个对点击事件的引用,但我无法做到。

$(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. 

      };  
     }); 

    }); 
+6

与其缠绕绑定/解除绑定事件,为什么不简单地检查轮播是否结束,然后什么都不做。 –

+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 
     } 
    }); 
});