Javascript if语句是否可以放在另一个if-else语句中?

Javascript if语句是否可以放在另一个if-else语句中?,javascript,if-statement,Javascript,If Statement,我觉得我应该能够从谷歌搜索中找到这个,但是没有,所以我会在这里问 我在第二条if语句中不断遇到错误,因此我想知道是否不允许在预先存在的if/else语句中放置另一条if语句 谢谢你的关注 function flipImages(){ currentImage = flipArray[i]; if (i == 6) { clearInterval(interval) } else { // add an opacity animat

我觉得我应该能够从谷歌搜索中找到这个,但是没有,所以我会在这里问

我在第二条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++;
    };              
}

您缺少了几个右括号和大括号,或者在错误的位置有一些右括号和大括号。使用带有语法高亮显示的体面编辑器可以很容易地发现这样的错误

对于记录,是的,如果语法正确,则可以嵌套
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++;
    };              
}
在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++;
    }
}

在hasglass('final')之后,您有一个缺少的字符。应该是hasClass('final')。如果允许,您只是有一个语法错误。您缺少一个