简体   繁体   English

循环触发点击事件

[英]Trigger click event in a loop

I am writing a bookmarklet to gather information from my Indiegala bundles I bought. 我正在编写一个书签,以从我购买的Indiegala捆绑包中收集信息。 Instead of gifting the whole bundle I am gifting onesies and twosies at a time so having the individual gift urls makes it easier. 我不是一次赠与整个捆绑包,而是一次赠予连体衣和二人一组,因此拥有单独的赠品网址会更容易。

Before they had it so the key was there and I could just gather it with a simple selector, now they have a little image you have to click on to get the gift url for each steam/desura game. 在他们拿到钥匙之前,我就可以用一个简单的选择器将其收集起来,现在他们有了一些图片,您必须单击才能获得每个Steam / Desura游戏的礼物URL。

I would like to, in a loop, click all of these gift images to get the gift url to appear and then loop through and get those gift urls. 我想循环单击所有这些礼物图像,以使礼物URL出现,然后循环浏览并获得这些礼物URL。 I wrote this: 我这样写:

var gifts = $('#icon-gift img');
for(var j=0; j < gifts.length-1; j++){
  gifts[j].click();
}

to loop through all the images and trigger the click so the gift url will appear. 循环浏览所有图像并触发点击,以便出现礼物网址。 I know it goes through each of the images because it logs to the console so I know it isn't getting stuck but for some reason it ONLY clicks the first image available. 我知道它会遍历每个图像,因为它会记录到控制台,所以我知道它不会卡住,但是由于某种原因,它仅单击了可用的第一个图像。 After you click an image (thankfully) it is clicked forever and when I refresh it only has the gift url then if I run the loop again it does the very next one (but that defeats the purpose of doing it in a script). 单击图像后(非常感激),它将被永久单击,并且在刷新时仅包含礼物网址,然后如果我再次运行循环,它将执行下一个图像(但这违背了在脚本中进行操作的目的)。

I have tried adding a delay(500) and even delay(1500) appended to the click() but that doesn't seem to change anything. 我尝试在click()添加一个delay(500)甚至delay(1500) ,但这似乎并没有改变。

My question is: does anyone know why this would happen and is there any advice on how to fix or get around this? 我的问题是:有人知道为什么会发生这种情况吗?是否有解决此问题的建议?

More thoughts and info 更多想法和信息

I think it has something to do with asynchronicity. 我认为这与异步性有关。 It's like the rest of the loop does everything else except for the other click event. 就像循环的其余部分一样,除了其他click事件之外,其他所有操作都没有。 I tried: var gifts = $('#icon-gift img'); 我试过了:var gifts = $('#icon-gift img');

$.each(gifts, function(i, val){
  setTimeout(function(){
    val.click();
    val.remove();
  }, 1000);
});

and it still only does the first one and removes all the other images. 并且它仍然只处理第一个图像,并删除所有其他图像。 If i manually go through gifts[0].remove(); 如果我手动检查gifts[0].remove(); then gifts[0].click(); 然后gifts[0].click(); it does the very next one. 它做下一个。 This is exactly what I thought the .each() or a for loop would do, but it seems like it can't execute the next click with the previous one still being executed or still present. 这正是我以为.each()for loop会做的事情,但是似乎无法执行下一次单击,而前一个仍在执行或仍然存在。

No need to use for loop 无需使用for loop

$('#icon-gift img').click();

is quite enough . 够了 It will perform a click on all the passed selectors #icon-gift img -> which are all the descendant images of #icon-gift . 它将对所有通过选择进行点击#icon-gift img - >这是所有后代图像#icon-gift All of them. 他们全部。

Otherwise you code was not working cause using gifts[j] you're actually extracting from the Array of your jQuery selectors the JS reference to the DOM HTML node element, therefore not any more a jQuery Object element. 否则,您的代码无法正常工作,原因是使用了gifts[j] ,实际上是从jQuery选择器的数组中提取对DOM HTML节点元素的JS引用,因此不再是jQuery Object元素。 Wrapping it again in an jQuery object would work $(gifts[j]) or using the eq() selector like gifts.eq(j) , but again not needed as I demonstrated above. 将其再次包装在jQuery对象中将可以使用$(gifts[j])或使用eq()选择器(如gifts.eq(j) ,但是如上所述,也不需要。

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

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