OnClick会打开新窗口,但也会更改当前窗口

时间:2013-12-03 20:19:55

标签: javascript onclick

我正在使用IE8。我设置了以下链接:

<a href="terms.html" onclick="window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=600')" />Terms & Conditions</a>

当我点击它。我按照我想要的方式弹出。但它也将当前窗口设置为相同的URL。我知道我错过了一些愚蠢的话。只是可以搞清楚。

谢谢!

2 个答案:

答案 0 :(得分:3)

在window.open之后添加 return false; ,如下所示:

<a href="terms.html" onclick="window.open(this.href, 'targetWindow', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=600');return false;" />Terms & Conditions</a>

答案 1 :(得分:1)

由于您点击了<a> - 链接,因此您的浏览器会自动关注该链接。您需要使onclick方法返回false,以使其无法在当前窗口中打开该页面。从HTML中提取所有JavaScript代码也很好,所以这是你应该研究的东西。但是,这个解决方案似乎可以解决问题:

<强> HTML

<a href="terms.html" onclick="return openWindow('terms.html')" />Terms & Conditions</a>

<强>的JavaScript

function openWindow(href) {
  window.open(href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=600');
  return false;
}