Javascript jQuery if语句中的多个条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10722682/
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
jQuery multiple conditions within if statement
提问by MG1
What is the syntax for this loop to skip over certain keys? The way I have it written is not working properly.
这个循环跳过某些键的语法是什么?我写的方式不能正常工作。
$.each(element, function(i, element_detail){
if (!(i == 'InvKey' && i == 'PostDate')) {
var detail = element_detail + ' ';
$('#showdata').append('<div class="field">' + i + detail + '</div>');
}
});
回答by CD Smith
Try
尝试
if (!(i == 'InvKey' || i == 'PostDate')) {
or
或者
if (i != 'InvKey' || i != 'PostDate') {
that says if i does not equals InvKey
OR PostDate
表示如果 i 不等于InvKey
ORPostDate
回答by SLaks
i == 'InvKey' && i == 'PostDate'
will never be true, since i
can never equal two different things at once.
i == 'InvKey' && i == 'PostDate'
永远不会是真的,因为i
永远不可能同时等于两个不同的东西。
You're probably trying to write
你可能正在尝试写
if (i !== 'InvKey' && i !== 'PostDate'))
回答by WTellos
A more general approach:
更通用的方法:
if ( ($("body").hasClass("homepage") || $("body").hasClass("contact")) && (theLanguage == 'en-gb') ) {
// Do something
}