简体   繁体   English

浏览器同时打开javascript:void(0)和onclick链接

[英]Browser opens both javascript:void(0) and onclick link

I have an image embedded within an anchor tag in HTML. 我在HTML的锚标记中嵌入了一张图片。 The problem is that, when I click the image, I have two tabs that open instead of one. 问题是,当我单击图像时,我打开了两个选项卡,而不是一个。

Problem : 问题

Two tabs are opened, one with the correct link, the other is a blank tab ( about:blank in the address bar). 将打开两个选项卡,一个选项卡具有正确的链接,另一个选项卡是空白选项卡(地址栏中的about:blank )。

Relevant sample code : 相关样本代码

<a href="javascript:void(0)" onclick="window.open('http://www.google.com');">
   <img src="..." />
</a>

What I've tried : 我尝试过的

Immediately, I see that the onclick event is missing return false; 立刻,我发现onclick事件丢失了, return false; and so I add it. 所以我添加了它。 Now the window.open function is followed by return false; 现在window.open函数后面跟着return false; , but the issue persists. ,但问题仍然存在。

I tried replacing the javascript:void(0); 我尝试替换javascript:void(0); with # in the href , but the issue remains - only this time instead of a blank tab, I get a new tab with the same content that I had in the tab where the link was clicked. href使用# ,但是问题仍然存在-只是这次而不是空白标签,我得到了一个新标签,其内容与单击链接的标签中的内容相同。

I also tried replacing the javascript:void(0) with javascript:; 我还尝试用javascript:void(0) javascript:;替换javascript:void(0) javascript:; , but that has the same result. ,但结果相同。

Additionally, I tried using event.preventDefault(); 另外,我尝试使用event.preventDefault(); - again, did not get the desired results. -同样,没有得到预期的结果。

Workaround : 解决方法

I've found that if I remove the href attribute and only have the onclick one, it works as expected. 我发现,如果删除href属性,而只有onclick一个,它将按预期工作。 While this delivers the wanted results, I don't really like it. 尽管这提供了想要的结果,但我并不真正喜欢它。 I also know that using javascript:void(0) is not considered good practice either, but I don't make all the decisions here. 我也知道使用javascript:void(0)也不被认为是一种好习惯,但是我在这里没有做出所有决定。

Points of note : 注意事项

  • This only happens on Firefox and IE (v27 and v11). 这仅在Firefox和IE(v27和v11)上发生。 In Chrome it works fine. 在Chrome中可以正常运行。
  • The code comes from an XSLT stylesheet, but the generated XHTML looks the same as the sample code. 该代码来自XSLT样式表,但是生成的XHTML看起来与示例代码相同。
  • <img> element does not contain anything that would affect this. <img>元素不包含任何会影响此内容的内容。

Do you folks have any idea why this may be happening? 你们是否知道为什么会这样? Any solutions? 有什么办法吗? I'd prefer not to use any 3rd party libraries, although jQuery is possible. 尽管可以使用jQuery,但我不希望使用任何第三方库。 Thanks in advance. 提前致谢。

If anyone comes across this, the problem was that the <img> tag had a deeply-enrooted onClick event handler attached to it. 如果有人遇到此问题,则问题在于<img>标记已附加了根深蒂固的onClick事件处理程序。 So whenever I clicked the <a> tag, I called two onclick actions - the one from the <a> tag, and the other from the <img> one. 因此,每当单击<a>标记时,我都会调用两个onclick动作-一个来自<a>标记,另一个来自<img> I solved this problem by calling a new JS function and attaching it to the <a> tag, passing event as a param, and the function contains the event.stopPropagation(); 我通过调用一个新的JS函数并将其附加到<a>标记并将事件作为参数传递来解决了此问题,并且该函数包含event.stopPropagation();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM