我正在寻找一种更简单的方法来处理这些多个 If Else 语句,有数百个。我猜最好只在 If Else 中使用另一个 For 循环,但请告诉我大家的建议,谢谢!
var hrefs = [];
var list = document.getElementsByTagName("a");
for (var i=0; i<list.length; i++) {
var hey = list[i].href;
hrefs.push(hey);
}
if(window.location == 'http://') {
tileid = <?php echo $numposts.''; ?>;
} else if (window.location == hrefs[7]) {
tileid = 0;
} else if (window.location == hrefs[8]) {
tileid = 1;
} else if (window.location == hrefs[9]) {
tileid = 2;
} else if (window.location == hrefs[10]) {
tileid = 3;
} else if (window.location == hrefs[11]) {
tileid = 4;
} else if (window.location == hrefs[12]) {
tileid = 5;
} else if (window.location == hrefs[13]) {
tileid = 6;
} else if (window.location == hrefs[14]) {
tileid = 7;
} else if (window.location == hrefs[15]) {
tileid = 8;
} else if (window.location == hrefs[16]) {
tileid = 9;
} else if (window.location == hrefs[17]) {
tileid = 10;
} else if (window.location == hrefs[18]) {
tileid = 11;
} else {
tileid = window.history.state.projid;
}