0

当我点击链接时,它会在 Firefox、Opera Mini 中打开两个弹出窗口……还有其他方法吗?

<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";
           links[i].href = links[i].addEventListener("click", function(){var windowObjectReference = window.open(str1)}, false);
           break;

        default:
           links[i].target="_blank";
           links[i].href= string;
    }  

 } 
 }

 </script> 

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

1 回答 1

0

您的代码中有双重操作。查看与我的版本的区别。

<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";
           links[i].href = str1;
           break;

        default:
           links[i].target="_blank";
           links[i].href= string;
    }  

 } 
 }

 </script> 

<a href="http://www.google.com/" target="-blank">www.google.com</a></br>
于 2013-06-28T19:30:48.190 回答