Javascript:使用 OR 操作数将单个值与多个值进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13959064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:19:03  来源:igfitidea点击:

Javascript: Comparing SINGLE Value Against MULTIPLE Values with OR Operands

javascriptif-statementoperands

提问by AlecPerkey

Possible Duplicate:
Check variable equality against a list of values
Javascript if statement with multiple permissible conditions

可能的重复:
根据值列表检查变量是否相等
Javascript if 语句具有多个允许条件

I must click the same 21 of 253 items (li) in a dropdown list (ul).

我必须在下拉列表 (ul) 中单击 253 个项目中的 21 个 (li)。

Scrolling I'll have to do this for the same list on 500+ pages, I figured I could Javascript inject each ul, loop through and click each li which happens to be one of the 21. It seems I cannot do something like

滚动 我必须为 500 多个页面上的同一个列表执行此操作,我想我可以通过 Javascript 注入每个 ul,循环并单击每个 li,它恰好是 21 个中的一个。看来我不能做类似的事情

 if(item[i] === ('aasdf'|'basdf'|'cwefw'|'asdfd'|'trehe'|'ferth'|'erthg'|'erthh'|'ierth'|'jeth'|'kerth'|'lerth'|'merth'|'psdfg'|'gregq'|'rsrgs'|'sress'|'srget'|'sergu'|'sdfgsv'))

Is there a syntactically cleaner way of writing this ugly if statement below?

是否有一种语法更清晰的方式来编写下面这个丑陋的 if 语句?

var item = document.getElementById('myDropdownList').getElementsByTagName('li');

for (i=0;i<item.length;i++){

    if(item[i].innerText === 'Argentina' | item[i].innerText === 'Australia' | item[i].innerText === 'Brazil' | item[i].innerText === 'Canada' | item[i].innerText === 'China' | item[i].innerText === 'Colombia' | item[i].innerText === 'France' | item[i].innerText === 'Germany' | item[i].innerText === 'Indonesia' | item[i].innerText === 'India' | item[i].innerText === 'Italy' | item[i].innerText === 'Japan' | item[i].innerText === 'Malaysia' | item[i].innerText === 'Mexico' | item[i].innerText === 'Philippines' | item[i].innerText === 'Russia' | item[i].innerText === 'South Africa' | item[i].innerText === 'Sweden' | item[i].innerText === 'Switzerland' | item[i].innerText === 'United Kingdom' | item[i].innerText === 'USA'){

    item[i].click();

    }

}

回答by Bergi

You could use a variable and multiple comparison, but that's still lengthy:

您可以使用变量和多重比较,但这仍然很长:

var text = item[i].innerText;
if (text === 'Argentina' | text === 'Australia' | text === 'Brazil' | text === 'Canada' | text === 'China' | text === 'Colombia' | text === 'France' | text === 'Germany' | text === 'Indonesia' | text === 'India' | text === 'Italy' | text === 'Japan' | text === 'Malaysia' | text === 'Mexico' | text === 'Philippines' | text === 'Russia' | text === 'South Africa' | text === 'Sweden' | text === 'Switzerland' | text === 'United Kingdom' | text === 'USA')

Or you could just use an array and check if the string is contained in it.

或者您可以只使用一个数组并检查字符串是否包含在其中。

var matches = ['Argentina','Australia','Brazil','Canada','China','Colombia','France','Germany','Indonesia','India','Italy','Japan','Malaysia','Mexico','Philippines','Russia','South Africa','Sweden','Switzerland','United Kingdom','USA'];
if (~ matches.indexOf(item[i].innerText) …

Yet, for the complicated != -1comparisonand the lack of native indexOfin older IEs, people tend to use regexes:

然而,对于复杂的!= -1比较indexOf旧 IE 中缺乏本机,人们倾向于使用正则表达式:

var regex = /Argentina|Australia|Brazil|Canada|China|Colombia|France|Germany|Indonesia|India|Italy|Japan|Malaysia|Mexico|Philippines|Russia|South Africa|Sweden|Switzerland|United Kingdom|USA/
if (regex.test(item[i].innerText)) …

回答by Brad Christie

var options = ['Argentina', 'Australia', 'Brazil', 'Canada', ...];
if (options.indexOf(item[i].innerText) !== -1){
  // item[i] was found in options
}

Something like that? use Array.indexOf(Unless I've mis-read the question? In which case post a comment and I'll do my best to re-work my answer)

类似的东西?使用Array.indexOf(除非我误读了问题?在这种情况下发表评论,我会尽力重新回答我的答案)