Javascript 对两个不同的ID使用jquery IF语句

Javascript 对两个不同的ID使用jquery IF语句,javascript,jquery,if-statement,Javascript,Jquery,If Statement,这可能非常简单,但我自己可以找到一个解决方案。我有以下代码: $('article li.active').each(function() { if ($(this).attr('id') == 'arrival-and-departure') { $('p.quot').hide(); } else if ($(this).attr('id') == 'additional-needs') {

这可能非常简单,但我自己可以找到一个解决方案。我有以下代码:

      $('article li.active').each(function() {
        if ($(this).attr('id') == 'arrival-and-departure') {
          $('p.quot').hide();
        }
        else if ($(this).attr('id') == 'additional-needs') {
          $('p.quot').hide();
        }
        else {$('p.quot').show()}
      };
我想知道如何将这两个如果结合起来,这样我就只需要IF和Else了。欢迎任何帮助,谢谢

您应该使用或:

$('article li.active').each(function() {
    if ($(this).attr('id') == 'arrival-and-departure' || 
        $(this).attr('id') == 'additional-needs') 
    {
      $('p.quot').hide();
    }
    else {$('p.quot').show()}
  };
您应该使用或:

$('article li.active').each(function() {
    if ($(this).attr('id') == 'arrival-and-departure' || 
        $(this).attr('id') == 'additional-needs') 
    {
      $('p.quot').hide();
    }
    else {$('p.quot').show()}
  };
“双管道”操作符充当“或”操作符。所以检查一下是这个还是那个


“双管道”操作符充当“或”操作符。因此,请检查是这个还是那个。

您可以使用&&and | |或

var a = true;
var b = false;
var c = true;
if(a&&b) //false since b=false, only a=true
if(a&&c) //true since a&c are both true
if(a||b)//a=true so>will be true. Javascript even won't check B (keep in mind when using functions!
详情如下:

您可以使用&&和| |或

var a = true;
var b = false;
var c = true;
if(a&&b) //false since b=false, only a=true
if(a&&c) //true since a&c are both true
if(a||b)//a=true so>will be true. Javascript even won't check B (keep in mind when using functions!
详情如下:

您也可以将if与| |运算符一起使用,如下所示:

$('article li.active').each(function() {
    switch ($(this).attr('id')) {
        case 'arrival-and-departure':
        case 'additional-needs':
            $('p.quot').hide();
            break;
        default:
            $('p.quot').show();
    }
});
您还可以使用| |运算符代替if,如下所示:

$('article li.active').each(function() {
    switch ($(this).attr('id')) {
        case 'arrival-and-departure':
        case 'additional-needs':
            $('p.quot').hide();
            break;
        default:
            $('p.quot').show();
    }
});

如果v1==test | | v1==other{…},则应在第一个if语句中使用或,如果v1==test | | v1==other{…},则应在第一个if语句中使用或