切换案例不在javascript中执行

时间:2013-07-03 14:14:59

标签: switch-statement case

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

<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>

1 个答案:

答案 0 :(得分: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);
}