【问题标题】:Exclude all other active IDs in if statement在 if 语句中排除所有其他活动 ID
【发布时间】:2017-09-19 13:56:28
【问题描述】:

我正在编写一个带有if 语句的简单函数,根据用户的选择重定向用户。基本上,用户会看到几个选项(A、B、C 等)。 and the user is redirect to one URL when say A and B are selected, and to another URL when say A and C are selected.

到目前为止,这是可行的,使用以下代码:

function redirect() {
    if (((document.getElementById('A').classList.contains('active')) == true) && ((document.getElementById('B').classList.contains('active')) == true)) {
        window.location = "https://www.example.com";
    };
};

我的问题是我还希望能够排除所有其他可能性,这意味着如果选择了 A 和 B,我想添加一个重定向用户的子句,但没有其他选项。有没有办法在不明确将所有其他选项列为('active')) == false 的情况下做到这一点?

再次感谢您,如果这个问题反映了我对编码的基本知识,我们深表歉意。

【问题讨论】:

  • 你需要明确检查
  • 尝试使用mapeverysome Array 方法。
  • 为什么不只是var a = (document.getElementById('A').classList.contains('active'))bc 类似。然后你检查if (a && b)

标签: javascript if-statement redirect conditional


【解决方案1】:

可以存储所有活动值:

const ids = ["A","B","C","D","E"],state = {};
for(const id of ids)
   state[id] = document.getElementById(id).classList.contains('active');

然后:

if( state.A && state.B)

【讨论】:

  • 这将如何排除所有其他ID?为什么不直接使用Array#mapArray#reduce
【解决方案2】:

列出所有具有active 类的元素并循环遍历它们。

// Get a list of all elements having the "active" class
var activeElements = document.querySelectorAll(".active");
var redirect = true; 

// Loop through all the elements having the active class
// and set redirect to false if one of them doesn't have one of the following ID: 'A' or 'B'
for (var i =0; i<activeElements.length;i++) {
    if(activeElements[i].id != 'A' && activeElements[i].id != 'B') {
        redirect = false;
    }
} 

//If the redirect variable is still true
if(redirect){
    redirect();
}

在这种情况下,您的redirect() 函数将使用Single Responsibility Principle

// url has a default value. So you can call the function these ways:
// redirect();   --> redirects to https://example.com
// redirect("https://stackoverflow.com"); --> redirects to StackOverflow.
function redirect(url = 'https://example.com') {
        window.location = url;
    };
};

【讨论】:

    【解决方案3】:
    var arr = [];
    var A = (document.getElementById('A').classList.contains('active')) == true)?arr.push('A'):false;
    var B = (document.getElementById('B').classList.contains('active')) == true)?arr.push('B'):false;
    var C = (document.getElementById('C').classList.contains('active')) == true)?arr.push('C'):false;
    
    if(arr.includes('A') && arr.includes('B') && arr.length ==2){
    
    }
    

    【讨论】:

    • 我想你的意思是arr.includes()(带s)
    • 为什么不只是var arr = ["A", "B", "C"].filter(id =&gt; document.getElementById(id).classList.contains("active"));var arr = Array.from(document.querySelectorAll(".active")).map(elem =&gt; elem.id)&amp;&amp; 链也可以替换为Array#every
    • 非常感谢,您能否解释一下 ?arr.push 方法的作用以及为什么将其设置为 false?
    • @Andre ?: 是三元运算符。
    • Sugumar 的回答和 Xufox 的替代方案似乎都有效,再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 2012-10-11
    • 2015-09-07
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 2016-11-11
    相关资源
    最近更新 更多