【问题标题】:how to click multiple buttons in a row with javascript?如何使用javascript连续单击多个按钮?
【发布时间】:2018-09-01 13:22:32
【问题描述】:

是否可以创建以 x 次点击时间间隔连续单击多个按钮的脚本?

例如,在单击 x 次后单击第一个按钮等。 (使用 Javascript)。

    var inputs = document.getElementsByClassName('className');
 for(var i=0; i<inputs.length;i++) {
       setInterval(function() 
    {inputs[i].click() },1000  
  } 

【问题讨论】:

  • 请贴出您尝试过的代码。
  • 当然这很有可能。向我们展示您的尝试以及遇到问题的地方

标签: javascript html button click intervals


【解决方案1】:

使用此代码

var allButtons = document.getElementsByClassName("button")
var timeInterval = 5000 // x time in miliseconds

function pressButton(iteration=0){
  
    setTimeout(function(){

      allButtons[iteration].click();
      pressButton(iteration++);

    }, timeInterval)
    
}

pressButton();


  
<div id="parent">

<button class="button" type="submit" > </button>
<button class="button" type="submit" > </button>
<button class="button" type="submit" > </button>
<button class="button" type="submit" > </button>
<button class="button" type="submit" > </button>

</div>

如果您粘贴代码,会有更多帮助

【讨论】:

    【解决方案2】:

      var clickcallback = function(i) {
        setTimeout(function() {
          let id = "button" + i;
          document.getElementById(id).click();
        }, 1000);   // one second
        if(i <= 3) {
            clickcallback(i+1);
        }
      };
    <div>
      <button id="button1" onClick="alert('click button1');">Button 1</button>
      <button id="button2" onClick="alert('click button2');">Button 2</button>
      <button id="button3" onClick="alert('click button3');">Button 3</button>
    </div>

    这里是演示: https://jsfiddle.net/frasim/730xmhfv/8/

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 1970-01-01
      • 2012-10-24
      • 1970-01-01
      • 2022-11-01
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      相关资源
      最近更新 更多