我需要创建一个按钮点击链接,并点击同样的链接。我测试的创建一个链接,并点击一个按钮点击
<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(制作下载按钮,链接可能会有所不同)。有没有其他的方式来做到这一点?
我refferd:How do I programmatically click a link with javascript?
和
How do I create a link using javascript?
为实现这一点,但没有成功。
你正在尝试getElementById(链接),但“链接”不是一个Id,它是一个元素。给链接一个id然后使用该id来获取它并点击()。 – nebulae
为什么你需要创建并点击链接?如果你已经有了URL,那么你可以将'window.location'设置为那个URL? – David