if语句可以放在另一个if else语句中吗?

时间:2013-02-12 16:32:28

标签: javascript if-statement

我觉得我应该可以通过谷歌搜索找到这个,但没有,所以我会在这里问。

我继续在第二个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++;
    };              
}

2 个答案:

答案 0 :(得分:1)

你遗漏了几个右括号和大括号,或者有一些在错误的位置。使用具有语法突出显示功能的合适编辑器可以轻松发现这样的错误。

对于记录,是的,可以嵌套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 :(得分:0)

你在if ($(currentImage).children().hasClass('final')之后错过了a)以及几个分号,这些分号构成了无效的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++;
    }
}

<强> Check it here