2013-02-12 101 views
-1

我覺得我應該能夠從谷歌搜索找到這個,但沒有,所以我會問這裏。if語句可以放在另一個if語句中嗎?

我不斷收到第二條if語句的錯誤,所以我想知道是否不允許在預先存在的if/else語句中放置另一個if語句。

感謝您的期待。

function flipImages(){ 
    currentImage = flipArray[i]; 

    if (i == 6) { 
     clearInterval(interval) 
    } 
    else { 
     // add an opacity animation to the flip so that it is less jarring 
     // set at a 100ms fade in 

     $(currentImage).animate({ 
      opacity: 1 
     }, 100, function() { 
      console.log(flipArray[i]); 
     } 

     // also animate in the child divs of the currentImage (which will only be text on 
     // the "final" div) 
     if ($(currentImage).children().hasClass('final'){ 
      $(currentImage).children().animate({ 
       opacity: 1, 
       left: '+=50' 
      }, 500, function(){ 
       console.log($(currentImage).children()); 
      }); 
     }); 
    ); 
    i++; 
    };    
} 
+1

你有hasClass後失蹤)( '最終')。應該是hasClass('final')) – 2013-02-12 16:33:42

+2

這是允許的,你只是有一個語法錯誤。你錯過了')' – 2013-02-12 16:33:51

回答

0

U've錯過了)。

function flipImages() { 
    currentImage = flipArray[i]; 

    if (i == 6) { 
     clearInterval(interval); 
    } else { 
     // add an opacity animation to the flip so that it is less jarring 
     // set at a 100ms fade in 

     $(currentImage).animate({ 
      opacity: 1 
     }, 100, function() { 
      console.log(flipArray[i]); 
     }); 

     // also animate in the child divs of the currentImage (which will only be text on 
     // the "final" div) 
     if ($(currentImage).children().hasClass('final')) { 
      $(currentImage).children().animate({ 
       opacity: 1, 
       left: '+=50' 
      }, 500, function() { 
       console.log($(currentImage).children()); 
      }); 
     } 
     i++; 
    } 
} 

Check it here

1

你錯過了幾個右括號和大括號,或者有一些錯誤的地方。使用語法突出的體面編輯器可以很容易地發現這樣的錯誤。

爲了記錄,是的,它可以嵌套if語句 - 假設你的語法是健全的。

這裏是你的代碼的修正版本:後if ($(currentImage).children().hasClass('final')也對夫婦的makesit無效JS分號

function flipImages(){ 
    currentImage = flipArray[i]; 

    if (i == 6) { 
     clearInterval(interval) 
    } 
    else { 
     // add an opacity animation to the flip so that it is less jarring 
     // set at a 100ms fade in 

     $(currentImage).animate({ 
      opacity: 1 
     }, 100, function() { 
      console.log(flipArray[i]); 
     }); 

     // also animate in the child divs of the currentImage (which will only be text on 
     // the "final" div) 
     if ($(currentImage).children().hasClass('final')) { 
      $(currentImage).children().animate({ 
       opacity: 1, 
       left: '+=50' 
      }, 500, function(){ 
       console.log($(currentImage).children()); 
      }); 
     }; 
     i++; 
    };    
} 
+0

感謝您的確認。 – 2013-02-12 16:47:51

+0

這是我的榮幸:) – jubair 2013-02-13 05:03:05