2012-04-10 71 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; 
}