2013-02-12 171 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