1

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

<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>

我需要这个,因为在单击按钮时,我将拨打 get 电话,我将收到一个 URL,然后我需要单击 URL(制作下载按钮,链接可能会有所不同)。有没有其他方法可以做到这一点?

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

如何使用 javascript 创建链接?

实现这一目标,但没有成功。

4

1 回答 1

7

看来您真的只是想更改页面的位置,而不是实际附加链接:

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();
于 2013-09-26T14:06:01.997 回答