请先看代码片段:
if(1>0){ window.location.href="a.html"; } if(1<0){ window.location.href="b.html"; }else{ window.location.href="c.html"; }为什么不跳转 a.html,反而跳转到了c.html
http://www.jb51.net/article/48504.htm
在每个跳转后面加一个return false;让它知道跳转了不需要在做其他事情。跳转到c.html是因为执行了后面的代码。
因为第一个if和第二个的else都执行了呗
要写成
if (1 > 0) {
window.location.href = "a.html";
} else if (1 < 0) {
window.location.href = "b.html";
} else {
window.location.href = "c.html";
}
不然 第一个if 和 第二个else 都需要执行,结果是执行了第一个if后,再执行第二个else,最终结果就是跳转c.html
要写成
if (1 > 0) {
window.location.href = "a.html";
} else if (1 < 0) {
window.location.href = "b.html";
} else {
window.location.href = "c.html";
}
不然 第一个if 和 第二个else 都需要执行,结果是执行了第一个if后,再执行第二个else,最终结果就是跳转c.html
最好不要有多个可以同时跳转的代码,要不执行哪个由浏览器控制,有些浏览器会执行最后的