简体   繁体   English

动态创建时,click()方法不会触发锚点上的click事件

[英]click() method not trigger click event on anchor when dynamically created

I have created an anchor element using document.createElement('a') method but while trying to trigger click event then it does not fire click event. 我已经使用document.createElement('a')方法创建了一个锚元素,但是在尝试触发click事件时,它不会触发click事件。 For more detail refer following code 有关更多详细信息,请参见以下代码

var link = document.createElement("a");
link.setAttribute("href", context+"/images/"+filePath);
link.setAttribute("href", "http://192.168.0.170:8082/abc/AttachmentData/getAttachment?path=pathTofile");
link.setAttribute("download", "abc.csv");
link.click(); 

I want to down load file .file link is provided in href. 我想下载文件。href中提供了文件链接。 This code is working on my local system but its not working when deployed on production server. 该代码在我的本地系统上有效,但是在生产服务器上部署时不起作用。

You just created the link element, but you have not added it to the document. 您刚刚创建了link元素,但尚未添加到文档中。

Try: 尝试:

document.body.appendChild(link); // put it where you want the link show.
link.click()

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

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