在JavaScript中压缩多个if语句

时间:2013-08-15 14:32:44

标签: javascript jquery syntax

我有这个:

if(currentSlide !== 3) {
    $('#chapterBackground.intro').fadeOut(100);
}

if(currentSlide !== 4) {
    $('#chapterBackground.intro').fadeOut(100);
}

...并且基本上想说如果currentSlide既不是3 OR 4,那么执行fadeOut函数。

6 个答案:

答案 0 :(得分:3)

if((currentSlide !== 3) || (currentSlide !== 4))  {
    $('#chapterBackground.intro').fadeOut(100);
}

道歉,编辑。

答案 1 :(得分:2)

您可以在if条件中使用OR运算符:

if(currentSlide !==3 || currentSlide !==4) {
    $('#chapterBackground.intro').fadeOut(100);
}

答案 2 :(得分:2)

你走了:

if(currentSlide !== 3 || currentSlide !== 4) {
    $('#chapterBackground.intro').fadeOut(100);
}

||表示“或”。对于“AND”,您将使用&&运算符;)

答案 3 :(得分:1)

if(currentSlide !== 3 || currentSlide !== 4) {
    $('#chapterBackground.intro').fadeOut(100);
}

答案 4 :(得分:0)

这就是我的:

if ( currentSlide + 1 >> 1 != 1 )

:d

答案 5 :(得分:0)

只是众多选择中的一个:

currentSlide = 3;

//The number would match the slide you want the transition to occur at
//So at slide 3 I want the intro to fade out
var slideIndexes = {
        3 : function() { $('#chapterBackground.intro').fadeOut(100); },
        16 : function() { $('#chapterBackground.presentation').fadeOut(100); },
        20 : function () { $('#chapterBackground.credits').fadeOut(100); }
};

if (slideIndexes[currentSlide]) slideIndexes[currentSlide]();

这个代码附带了一个漂亮的jsfiddle

相关问题