2012-04-10 72 views
0

有人能告诉我为什么return false不工作?我只想检查电流是否是黄色。如果是黄色课,不要做任何事(返回false)。问题是当你点击一个按钮时,它会再次运行它,但我想避免这种情况。 Here's the Fiddle of the problem返回假不工作

/*INSIDE THIS CODE RETURN FALSE NOT WORKING!!*/ 
$('.yellowcontroll').click(function(){ 

    if($yellow.after('.current').length){ 
     $('.yellow').show(); 
     $('.slideswrapper:not(:animated)').animate({'marginLeft':'-=100'},1000,function(){ 
      $('.current').removeClass('current').hide(); 
      $('.yellow').addClass('current'); 
      $('.slideswrapper').css({'margin-left':'0px'}); 

      if($('.current').is($('.slideswrapper div:last'))){ 
       $('.blue,.green,.red').clone().insertAfter('.slideswrapper div:last'); 
      } 

      if($('.current').is($('.red'))){ 
       $('.red').prevAll().remove(':not(.yellow)'); 
       $('.yellow').insertAfter($('.slideswrapper div:last')); 
      } 

      /*THIS IS NOT WORKING AS EXPECTED!!*/  
      if($('.current').is($('.yellow'))){ 
       return false; 
      } 
     }); 
    } 

}); 
+0

您是否尝试过在那里你期待返回一个错误的行中添加的console.log()?它是否达到了你的代码的那一部分? – 2012-04-10 17:05:03

+0

在发布之前请格式化您的代码,以便我们更容易阅读和理解。 – 2012-04-10 17:12:07

回答

2

问题是,您从回调中返回false到您的动画,而不是事件回调。

如果你正在寻找的是什么,当你点击第二次,那么你可以移动的状态,返回false,点击回调前发生:

$('.yellowcontroll').click(function(){ 

    /* MOVE THIS TO THE BEGINNING OF THE CLICK CALLBACK */  
    if($('.current').is($('.yellow'))){ 
     return false; 
    } 

    if($yellow.after('.current').length){ 
     $('.yellow').show(); 
     $('.slideswrapper:not(:animated)').animate({'marginLeft':'-=100'},1000,function(){ 
      $('.current').removeClass('current').hide(); 
      $('.yellow').addClass('current'); 
      $('.slideswrapper').css({'margin-left':'0px'}); 

      if($('.current').is($('.slideswrapper div:last'))){ 
       $('.blue,.green,.red').clone().insertAfter('.slideswrapper div:last'); 
      } 

      if($('.current').is($('.red'))){ 
       $('.red').prevAll().remove(':not(.yellow)'); 
       $('.yellow').insertAfter($('.slideswrapper div:last')); 
      } 
     }); 
    } 

}); 
+0

那么我该如何解决呢? – 2012-04-10 17:07:56

+0

不确定你想要做什么...我会用一种可能性更新。 – Prestaul 2012-04-10 17:08:46

+0

我已经更新了一个可能的解决方案,但我不能确定这是什么意向行为... – Prestaul 2012-04-10 17:11:32

1

在提琴的代码是一团糟,但根据你的问题,你似乎只是把你的逻辑放在了错误的地方。试着将你的return false逻辑在点击事件的顶部:

$('.yellowcontroll').click(function(){ 
    if($('.current').is($('.yellow'))){ 
     return false; 
    } 
    ... 
    }); 

This fiddle应该做你想要什么。

1

我想你想把他们的代码片段单击处理程序的开头:

http://jsfiddle.net/mihaifm/3YLEg/2/

$('.yellowcontroll').click(function(){ 
      /*THIS IS NOT WORKING AS EXPECTED!!*/  
      if($('.current').is($('.yellow'))){ 
       return false; 
       } 
1

移动有条件的虚假代码,点击处理程序的开头,如下面和使用hasClass如下所示。

DEMO

if ($('.current').hasClass('yellow')) { 
     return false; 
    } 
1
if($('.current').is('.yellow')){ 
    return false; 
}