我希望在位置href中使用switch case语句来处理我的不同条件。我试图避免继续写if语句
if (/red/.test(self.location.href)){
//do this red
}
if (/blue/.test(self.location.href)){
//do this blue
}
if (/yello/.test(self.location.href)){
//do this yellow
}
答案 0 :(得分:3)
var _url = window.location.href;
swith(true){
case /red/.test(_url):
//do red
break;
case /blue/.test(_url):
//do blue
break;
case /yellow/.test(_url):
// do yellow
break;
}