简体   繁体   English

使用Chrome在JQuery中模拟链接点击-使用href和download

[英]Simulating a link click in JQuery with Chrome - using both href and download

I have a Chrome extension that dynamically creates links and adds them to a webpage. 我有一个Chrome扩展程序,可以动态创建链接并将其添加到网页。 The links download files. 链接下载文件。 They each have a download attribute to name the files correctly (only works in Chrome, afaik - but it only needs to), as well as href for the url of the file. 它们每个都有一个download属性,可以正确命名文件(仅适用于Chrome,afaik-但仅需要命名),以及href作为文件的url。 How can I simulate clicking one of these links? 如何模拟单击这些链接之一?

I don't need to click the version within the document necessarily, just the link object. 我不必单击文档中的版本,只需单击链接对象即可。

Basically, how can I write, in Javascript/JQuery, code to have Chrome download a file named with download at the href location? 基本上,我怎么能写,在Javascript / jQuery的代码让Chrome浏览器下载一个名为文件downloadhref位置?

The JQuery click() command didn't seem to do anything, though I can't figure out why. JQuery click()命令似乎没有执行任何操作,尽管我不知道为什么。

I know this thread is old, but it's the first result in search, so for others who seeks answer, here it is: 我知道这个线程很旧,但是它是搜索的第一个结果,因此对于其他寻求答案的人来说,这里是:

$(css_selector)[0].click();

And you're doing it $(function(){ HERE }); 并且您正在执行$(function(){ HERE }); and some js functionality is added to css_selector on original page load, then maybe you want to delay HERE execution by setting timeout. 并且在原始页面加载时将某些js功能添加到了css_selector中,那么也许您想通过设置超时来延迟HERE执行。

you can try simulating the click like this 您可以尝试像这样模拟点击

$("your_selector").trigger("click");  

or like this 或像这样

$("your_selector").click();  

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

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