简体   繁体   English

如何使用JavaScript循环按下按钮

[英]How to push a button with javascript loop

I am searching for a way with javascript to run a code to the console of chrome and push all the buttons with the same CLASS name of a website 我正在寻找一种使用javascript来运行代码到chrome控制台并按与网站相同CLASS名称的所有按钮的方法

the code of the button is this 按钮的代码是这个

<span><a class="UFILikeLink" href="#" role="button" aria-live="polite" title="Like this" data-ft="{&quot;tn&quot;:&quot;>&quot;}" data-reactid=".3k">
  <i class="UFILikeLinkIcon img sp_j2jvp3kCzmm sx_751c68" data-reactid=".3k.0"></i>
  <span data-reactid=".3k.1">Like</span>
</a></span>

I have tried this code here 我在这里尝试过此代码

var el = document.getElementsByClassName('UFILikeLink'); 

for (var i=0;i<el.length; i++) {
    el[i].click();
}

Sorry my mistake. 抱歉,是我的错。

The code is working but if I run the code for the second time it pushes again the button and it's like before. 代码正在工作,但是如果我第二次运行代码,它将再次按下按钮,就像以前一样。

The thing is that I want to make it like, if the button is pushed once it will not be pushed again. 事实是,我想使它看起来像,如果一次按下按钮将不会再次按下它。

Again sorry for the mistake. 再次为这个错误感到抱歉。

If I push the button then the code of the webpage changes to this 如果我按下按钮,则网页代码将更改为此

<span><a class="UFILikeLink UFILinkBright" href="#" role="button" aria-live="polite" title="Unlike this" data-ft="{&quot;tn&quot;:&quot;?&quot;}" data-reactid=".sk">
  <i class="UFILikeLinkIcon img sp_j2jvp3kCzmm sx_c04d59" data-reactid=".sk.0"></i>
  <span data-reactid=".sk.1">Like</span>
</a></span>

You have jquery tag on your question, so here is how you would do it with jQuery: 您的问题上有jquery标记,因此这是使用jQuery的方法:

$(document).ready(function(){

    $(".UFILikeLink").each(function(){
        $(this).click();
    });

});

This will click on everything that has the class UFILikeLink when the page loads. 页面加载时,将单击所有具有UFILikeLink类的UFILikeLink

You don't need to loop through the selected elements, if you're using jquery. 如果您使用的是jquery,则无需遍历所选元素。 Simply write this 简单地写这个

$('.UFILikeLink').trigger('click');

But it seems like you're using pure js. 但似乎您正在使用纯js。 So in that case, you're gonna have to do something like this. 因此,在这种情况下,您将必须执行类似的操作。

var els = document.getElementsByClassName('test');

var event = new MouseEvent('click', {
    'view': window,
    'bubbles': true,
    'cancelable': true
  });

for(var el in els){
    els[el].dispatchEvent(event);
}

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

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