简体   繁体   English

锚点击功能在 Firefox 中不起作用

[英]anchors click function not working in firefox

This code:这段代码:

a = document.createElement('a')
a.setAttribute('href','http://www.google.de')
a.click()

works in chrome.在铬中工作。 It open www.google.de as expected.它按预期打开 www.google.de。 But in firefox it does nothing.但在 Firefox 中它什么都不做。 Why and how can it be made to work?为什么以及如何让它发挥作用?

I am using firefox 40.0.3 on ubuntu linux 15.04.我在 ubuntu linux 15.04 上使用 firefox 40.0.3。

Use the following:使用以下内容:

var a = document.createElement('a')
a.setAttribute('href','http://www.google.de');
document.getElementsByTagName('body')[0].appendChild(a);
a.click();

Firefox probably doesn't open the link because you never add it to the DOM. Firefox 可能不会打开链接,因为您从未将它添加到 DOM。

You could add the element to the DOM and use css display:none to hide it from the page.您可以将元素添加到 DOM 并使用 css display:none将其从页面中隐藏。

However, a more standard approach would be to either use the javascript window.open() method or window.location.href depending on your desired behavior.但是,更标准的方法是根据您所需的行为使用 javascript window.open()方法或window.location.href

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

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