在JavaScript中压缩多个if语句

在JavaScript中压缩多个if语句,javascript,jquery,syntax,Javascript,Jquery,Syntax,我有这个: if(currentSlide !== 3) { $('#chapterBackground.intro').fadeOut(100); } if(currentSlide !== 4) { $('#chapterBackground.intro').fadeOut(100); } …如果currentSlide不是3或4,则执行淡出功能 if((currentSlide !== 3) || (currentSlide !== 4)) { $('#cha

我有这个:

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

if(currentSlide !== 4) {
    $('#chapterBackground.intro').fadeOut(100);
}
…如果currentSlide不是3或4,则执行淡出功能

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

道歉,编辑

您可以在单个if条件下使用运算符:

if(currentSlide !==3 || currentSlide !==4) {
    $('#chapterBackground.intro').fadeOut(100);
}
您可以在单个if条件下使用运算符:

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

if(currentSlide !== 3 || currentSlide !== 4) {
    $('#chapterBackground.intro').fadeOut(100);
}
| |的意思是或。对于和,您将使用&&运算符

给你:

if(currentSlide !== 3 || currentSlide !== 4) {
    $('#chapterBackground.intro').fadeOut(100);
}
| |的意思是或。对于和,您将使用&&运算符

下面是我的:

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

这是我的:

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

:D

只是众多选项之一:

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]();

这段代码附带了一个漂亮的选项。

这只是众多选项中的一个:

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]();

这段代码附带了一个漂亮的提示。

避免使用==和!=只要可能,即几乎100%的时间。您也可以通过删除内括号来简化代码。避免使用==和!=只要可能,即几乎100%的时间。您还可以通过删除内括号来简化代码。您希望实现什么?如前所述,淡出将发生在每张幻灯片上,因为这些测试中至少有一个总是正确的。您希望实现什么?如前所述,每个幻灯片上都会出现淡出,因为这些测试中至少有一个总是正确的;没有任何if。或者,它与$'chapterBackground.inro'。fadeout 100;没有任何假设。