简体   繁体   English

否则if语句在jquery中不起作用

[英]else if statement not working in jquery

I removed the variable names and just put the values in for simplicities sake. 我删除了变量名,只是为了简单起见将它们放在其中。

Can't work out why this doesn't work properly, the else statement works fine, the if statement works fine but the else if part doesn't. 无法弄清楚为什么它不能正常工作, else语句可以正常工作, if语句可以正常工作,而else if部分不能正常工作。

 if (scrollTop > 200) {

    $('.vote-next-hover').css({

        position: 'fixed',
        top: '50px',
        width: statuscontain,

    });

 } 

 else if (scrollTop > 5500) {

     $('.vote-next-hover').css({

        position: 'absolute',
        top: '',
        width: '',

     });

 } 

else {

  $('.vote-next-hover').css({

        position: 'relative',
        top: '',
        width: '',
    });

}   

Scroll 200px, fires the if statement, scroll back to less than 200 and it fires the else statement, but at no point does it fire the else if statement. 滚动200px,触发if语句,向后滚动到小于200并触发else语句,但是在任何时候都不会触发else if语句。

I thought it would sort of be - Condition 1 met, fires - Condition 2 met, fires - Condition 3 met, fires? 我以为是-条件1遇难,火灾-条件2遇难,火灾-条件3遇难,火灾?

You should swap the if and else if clauses. 您应该交换ifelse if子句。 See, if scrollTop value is greater than 5500 , it's definitely greater than 200 - therefore it will pass the very first check quite well, making the second one meaningless. 请参见,如果scrollTop值大于5500 ,则它肯定大于200因此它将很好地通过第一个检查,从而使第二个检查毫无意义。

So if (scrollTop > 5500) should go first in your code, followed by else if (scrollTop > 200) check. 因此, if (scrollTop > 5500)应该在代码中排在首位,然后再进行else if (scrollTop > 200)检查。


I wonder did you know that the same branching logic could be written with... switch ? 我不知道您是否知道可以使用... switch编写相同的分支逻辑?

var hoverStyle = { position: 'relative', top: '', width: '' };
switch (true) {
  case scrollTop > 5500:
    hoverStyle.position = 'absolute';
    break;
  case scrollTop > 200:
    hoverStyle = { position: 'fixed', top: '50px', width: statuscontain };
}
$('.vote-next-hover').css(hoverStyle);

Some people even consider it more readable than if-elseif-else . 有些人甚至认为它比if-elseif-else更具可读性。 But of course the same restrictions apply there - the less common cases should go first (or should be re-checked later). 但当然,这里也有同样的限制-较不常见的情况应首先处理(或以后再检查)。

As a sidenote, I really think there's no point duplicating $('.vote-next-hover').css() call within the branches. 作为一个旁注,我真的认为在分支内复制$('.vote-next-hover').css()调用没有意义。 It's enough to separate only different parts of code - in this case, setting up the .css() param. 仅分隔代码的不同部分就足够了-在这种情况下,设置.css()参数。

The if/else if/else block should only run one of the three choices. if / else if / else块应仅运行以下三个选项之一。 For example: 例如:

var test = 4;
if (test === 4) { //true
    test = 3;
} else if(test === 3) {//does not do, already did one
    test = 0
} else {
    test = “something”
}

So you need three if blocks, not if/else if/else. 因此,您需要三个if块,而不是if / else if / else。

Say you have scrolled to 6000, 6000 is greater than 200, so the first condition is met. 假设您滚动到6000,则6000大于200,因此满足第一个条件。 Then because you have an else if it doesn't test the second condition. 然后,因为else if不测试第二个条件,则有另一个。

You have two options: 您有两种选择:

Add the scrollTop > 5500 check to inside the > 200 check, or replace the order of the if, else if to put the > 5500 check first, then then > 200 check. 将scrollTop> 5500 check添加到> 200 check内,或者替换if, else if的顺序if, else if先放置> 5500 check,然后再放置> 200 check。

if (scrollTop > 200) {
    if (scrollTop > 5500) {
        $('.vote - next - hover ').css({
            position: 'absolute ',
            top: '',
            width: '',
        });
    } else {
        $('.vote - next - hover ').css({
            position: 'fixed ',
            top: '50px ',
            width: statuscontain,
        });
    }
} else {
    $('.vote - next - hover ').css({
        position: 'relative ',
        top: '',
        width: '',
    });
}

"Scroll 200px, fires the if statement, scroll back to less than 200 and it fires the else statement, but at no point does it fire the else if statement." “滚动200px,触发if语句,向后滚动到小于200并触发else语句,但是在任何时候都不会触发else if语句。”

This is a logic error. 这是一个逻辑错误。 Since any number higher than 5500 is also higher than 200, and when you use else if you're saying that the first criteria was not true. 因为任何高于5500的数字也高于200,并且当您使用else时,如果您说第一个条件不正确。

You should change your code to: 您应该将代码更改为:

if (scrollTop > 5500) {
    $('.vote-next-hover').css({
        position: 'absolute',
        top: '',
        width: '',
    });    
}
else if (scrollTop > 200) {                      
    $('.vote-next-hover').css({
        position: 'fixed',
        top: '50px',
        width: statuscontain,
    });

}
else {
    $('.vote-next-hover').css({

        position: 'relative',
        top: '',
        width: '',
    });    
}

This logic error 此逻辑错误

$('#crud_interval').bind("change", function() {

if( $('#crud_interval').val() ==='Daily' ) { //not running
    $('#crud_weekly_day').hide();
    $('#crud_monthly_date').hide();
} else if( $('#crud_interval').val() ==='Weekly' ) { //not running
    $('#crud_weekly_day').show();
    $('#crud_monthly_date').hide();
} else {
    $('#crud_weekly_day').hide();
    $('#crud_monthly_date').show();
} 

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM