简体   繁体   中英

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

The JQuery click() command didn't seem to do anything, though I can't figure out why.

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 }); and some js functionality is added to css_selector on original page load, then maybe you want to delay HERE execution by setting timeout.

you can try simulating the click like this

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

or like this

$("your_selector").click();  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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