0

我已经为重定向创建了这个脚本。它可以正常工作以匹配大小写,但不执行切换案例。代码有问题吗?

<script type="text/javascript"> 
window.onload = function () {
    var elements = document.getElementsByTagName('a');
    for (var i = 0; i < elements.length; i++) {
    elements[i].href= escape(elements[i].href);
        (function(index){ 
            elements[index].onclick = function () {
            var string = elements[index].href;          //href value
            var patt1=/\bwww.google.com/g;
            var n=string.match(patt1);
            switch (n) {
            case "www.google.com":
                var red = "http://yahoo.com";
                elements[index].target = "_blank";
                elements[index].href = red;

                break;

            default:
                elements[index].href = unescape(string);
            }
        }
        })(i);

    }
}
 </script> 
<a href="http://www.google.com/" target="-blank">www.google.com</a></br>
4

1 回答 1

0

你可以试试这个:

switch (true) {
    case n == "www.google.com":
        console.log(n);
        var red = "http://yahoo.com";
        elements[index].target = "_blank";
        elements[index].href = red;

        break;

    default:
        elements[index].href = unescape(string);
}
于 2013-07-03T15:01:00.337 回答