具有多个条件的多个if语句?

时间:2016-10-11 18:51:20

标签: jquery if-statement conditional

我正在尝试检查我的网页的<title>标记是否包含某些关键字,以显示/隐藏div。每个州(4个州)在标题中有4个不同的关键词。我正在试图弄清楚如何编写我的if语句,所以它说

  

如果标题有“单词1”或“单词2”或“单词3”或“单词4”,则执行此功能

目前,我写的是这样的:

if ($("title").text() == "Marketing" || "Marketing Product Support" || "Inbound Marketing Best Practices" || "Marketing Certification Help") {
$('.marketing-board-header').show();
}

if ($("title").text() == "Sales" || "Sales Product Support" || "Inbound Sales Best Practices" || "Sales Certification Help") {
$('.sales-board-header').show();
}

if ($("title").text() == "COS Design" || "COS Design Support" || "Share Your Work" || "Design Certification Help") {
$('.design-board-header').show();
}

if ($("title").text() == "community Ideas") {
$('.idea-board-header').show();
}

但这似乎并没有隐藏/显示我想要的div,我错过了什么?或者有更好的方法吗?

感谢您的帮助!

5 个答案:

答案 0 :(得分:1)

我可能会使用switch语句。

jQuery(document).ready(function(){
    switch ( jQuery('title').text() )
    {
       case "Marketing":
       case "Marketing Product Support":
       case "Inbound Marketing Best Practices": 
           jQuery('.marketing-board-header').show();
           break;
       case "Sales":
       case "Sales Product Support":
       case "Inbound Sales Best Practices": 
           jQuery('.sales-board-header').show();
           break;
    }
});

等...

答案 1 :(得分:0)

您正在尝试一次又一次地搜索DOM,这是一种不好的做法,也是代价高昂的。

尝试获取一次标题并将该变量用于多次检查 你的if条件也有错误 它无法检查你的方式 将代码修改为以下代码段

$(document).ready(function(){
  var title=$("title").text() ;
 if(title==="Marketing" || title=="Marketing Product Support" || title== "Inbound Marketing Best title==Practices" || title== "Marketing Certification Help"){
      $('.marketing-board-header').show();
}

});

希望这有帮助

答案 2 :(得分:0)

如果$("title").text()确实为您提供了您想要比较的价值,那么您的条件将始终为true。因为在OR条件下,您提供的字符串总是计算为true。正确的方法是 -

var title = $("title").text();
if (title == "Marketing" || title == "Marketing Product Support" || title == "Inbound Marketing Best Practices" || title == "Marketing Certification Help") {
    $('.marketing-board-header').show();
}

if (title == "Sales" || title == "Sales Product Support" || title == "Inbound Sales Best Practices" || title == "Sales Certification Help") {
    $('.sales-board-header').show();
}

if (title == "COS Design" || title == "COS Design Support" || title == "Share Your Work" || title == "Design Certification Help") {
    $('.design-board-header').show();
}

if (title == "community Ideas") {
    $('.idea-board-header').show();
}

希望这有帮助!

答案 3 :(得分:0)

你应该写点像

import { Component, Input, trigger, state, style, transition, animate } from '@angular/core';
@Component({
  selector: 'basic-component',
  template: `
    <div class="well" [@dataState]="active" (click)="toggleState()" style="cursor:pointer">SOME TEXT</div>
  `,
  animations: [
    trigger('dataState', [
      state('inactive', style({
        backgroundColor: '#eee',
        transform: 'scale(1)'
      })),
      state('active',   style({
        backgroundColor: '#cfd8dc',
        transform: 'scale(1.5)'
      })),
      transition('inactive => active', animate('100ms ease-in')),
      transition('active => inactive', animate('100ms ease-out'))
    ])
  ] 
})
export class BasicComponent {
  active = 'inactive';
  toggleState() {
    this.state = (this.state === 'active' ? 'inactive' : 'active');
  }
}

但是你写了

if (A==B || A==C || A==D){/*...*/}

在这种情况下,C被评估为布尔语句本身(true / false),如果字符串始终为true。 所以你以某种方式写作

if (A==B || C || D)

答案 4 :(得分:0)

您遇到的问题是每个'或'运算符||应该将整个条件分开。您的代码首先评估可能返回true或false的title == 'word1',但它会尝试在没有条件的情况下评估word2,因此它返回true。因此,修正后的版本如下:

if(title == 'word1' || title == 'word2'){
    /* code for word1 or word2 here */
}

一种可能的解决方案是使用带有连贯的switch语句。这是它的外观。

var title = $("title").text();
switch(title){
    case "word1":
    case "word2":
    case "word3":
        /* code for word1, word2, or word3 here */
        break;
    case "word4":
    case "word5":
        /* code for word4 or word5 here */
        break;
}

因此,您在第一个中断上方编写的代码仅适用于“word1,word2或word3”。这比if语句更容易辨认。