[英]How to trigger click event on all buttons with a specific class in jquery
I would like to know how to trigger click event on all download buttons in jquery. 我想知道如何在jquery中的所有下载按钮上触发click事件。
I am using WebShare from Share it and i would like to download a folder at a time because it contains many files and I can't go on downloading each file. 我正在使用“共享它”中的WebShare,我想一次下载一个文件夹,因为它包含许多文件,并且我无法继续下载每个文件。
I tried using Downthemall but they are buttons and not links. 我尝试使用Downthemall,但它们是按钮而不是链接。 When they are clicked the Download gets started.
单击它们后,便开始下载。
Anyway, all the download buttons have the class button-download
无论如何,所有下载按钮都具有class
button-download
and I tried the following 我尝试了以下
var reversed = $("button.button-download").get().reverse();
$(reversed).each(function () {
$(this).trigger("click").delay(100);
});
But that downloads only the first file. 但这只会下载第一个文件。 I have tried delay() so that for every download, there will be a delay so that Firefox won't get stuck.
我尝试过delay(),以便每次下载都会有一个延迟,以便Firefox不会卡住。 But still, it downloads only the first one and leaves the rest (ie doesn't click on the rest of the buttons)
但仍然,它仅下载第一个,其余部分保留下来(即不单击其余按钮)
I am executing the script in Inspector console of firefox. 我正在firefox的Inspector控制台中执行脚本。
PS I have looked at available answers at Stackoverflow but they didn't seem to work for me. PS:我已经查看了Stackoverflow上的可用答案,但它们似乎对我没有用。
delay
only affects animation operations. delay
只会影响动画操作。
If you want to trigger click on each of them in sequence , with a delay in-between, you can use setTimeout
: 如果要触发它们依次单击,并且之间有一定的延迟,则可以使用
setTimeout
:
$("button.button-download").each(function(i) {
var btn = $(this);
setTimeout(btn.trigger.bind(btn, "click"), i * 100);
});
That queues a bunch of timers, one every 100ms, and calls trigger("click")
on each button as the timers fire. 这使一排计时器排队,每100毫秒一个,并在计时器
trigger("click")
在每个按钮上调用trigger("click")
。
Whether that will actually cause the downloads the way you want is another question, but that will successfully click the buttons. 这是否会真正导致您想要的下载方式是另一个问题,但这将成功单击按钮。
Example without downloading, and with a longer delay so you can see it running more easily: 该示例无需下载,并且延迟时间更长,因此您可以看到它更容易运行:
$("button.button-download").click(function() { console.log(this.innerHTML + " clicked"); }); $("button.button-download").each(function(i) { var btn = $(this); setTimeout(btn.trigger.bind(btn, "click"), i * 500); });
<button class="button-download">one</button> <button class="button-download">two</button> <button class="button-download">three</button> <button class="button-download">four</button> <button class="button-download">five</button> <button class="button-download">six</button> <button class="button-download">seven</button> <button class="button-download">eight</button> <button class="button-download">nine</button> <button class="button-download">ten</button> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Try this: 尝试这个:
$('.button-download').each(function(i, obj) {
obj.trigger("click");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.