我已经为链接重定向编写了这段代码,但是代码的问题它只触发了第一种情况,它在 switch 语句中省略了其他情况
<script type="text/javascript">
window.onload = function () {
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
var string = links[i].href; //href value
var str = string;
var spl = string.split("/");
switch (spl[2]) {
case 'www.google.com':
var str1 = "http://yahoo.com";
links[i].target = "_blank";
var m = links[i].addEventListener("mouseup", function () {
window.open(str1)
}, false);
return (m);
break;
case 'www.ebay.com':
var str1 = "http://yahoo.com";
links[i].target = "_blank";
var m = links[i].addEventListener("mouseup", function () {
window.open(str1)
}, false);
return (m);
break;
default:
links[i].href = string;
}
}
}
</script>
HTML:
<a href="http://www.google.com/">www.google.com</a></br>
<a href="http://www.ebay.com/">www.ebay.com</a></br>