创建一个链接,然后单击一个按钮即可单击它

Create a link and click it on a single button click

本文关键字:一个 单击 按钮 链接 创建 然后      更新时间:2023-09-26

我需要在单击按钮时创建一个链接,并且还要单击该链接。我正在测试上面提到的方法

<script type="text/javascript">
function download()
{
  alert("Hello");
  var link=document.createElement("a");
  link.href="https://stackoverflow.com/questions/4772774/how-do-i-create-a-link-using-   javascript";
document.getElementById(link).click();
// here it says "cannot click on null", means id is not link, then how can i obtain the id
alert("Hello again");
}
</script>
<button type ="button" value="Download" onClick="download();">Downloads</button>
我需要这个,因为,单击按钮

,我将进行呼叫,并且您将收到一个URL,然后我需要单击URL("下载"按钮,链接可能会有所不同)。还有其他方法可以做到这一点吗?

我参考:如何以编程方式单击带有javascript的链接?

如何使用 javascript 创建链接?

为此,但没有成功。

似乎您真的只想更改页面的位置,而实际上根本不附加链接:

location.href = 'your link you got back'

如果您确实希望在页面上添加物理链接:

var link=document.createElement("a");
link.id = 'someLink'; //give it an ID!
link.href="http://stackoverflow.com/questions/4772774/how-do-i-create-a-link-using-   javascript";
//Add the link somewhere, an appendChild statement will do.
//Then run this
document.getElementById('someLink').click();