当我点击链接时,它会在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>
答案 0 :(得分: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>