以下是否有简写 -
if(tld == "com" || tld == "net" || tld == "co" || tld == "org" || tld == "info" || tld == "biz")
{
//do something;
}
以下是否有简写 -
if(tld == "com" || tld == "net" || tld == "co" || tld == "org" || tld == "info" || tld == "biz")
{
//do something;
}
你可以使用一个数组
if(["","com","net","co","org","info","biz"].indexOf(tld) > -1) {
// do something
}
或者如果您使用的是 jquery:
$.inArray(tld, ["com","net","co","org","info","biz"])
使用正则表达式:
if ( /^(com|net|co|org|info|biz)$/i.test(tld) ) {
// do something
}
您是否考虑过使用 switch 语句?像这样的东西:
switch(tld)
{
case 'com':
case 'net':
case 'co':
...
...
// do something for all of them
break;
default:
// if you want you can have default process here
break;
}
if (~["com", "net", "co", "org", "info", "biz"].indexOf(method)){/*Do somthing if true*/}
if (!~["com", "net", "co", "org", "info", "biz"].indexOf(method)){/*Do somthing if false*/}