【问题标题】:Make a button click itself让按钮自己点击
【发布时间】:2017-04-23 12:46:13
【问题描述】:

我只想让我可以将脚本粘贴到 f12 开发控制台中,并且具有特定 id 的按钮将自行单击 10 次。

【问题讨论】:

标签: javascript button console


【解决方案1】:

您可以像这样使用纯 JavaScript 来实现这一点:

  • 您可以使用 setTimeout() 让您有时间将您的 sn-p 粘贴到控制台中。
  • 之后可以使用 setInterval() 来执行按钮点击指定的时间间隔。(需要清除时间间隔才能停止执行)
  • 您使用 document.getElementById("button-id").click() 来点击您的按钮。

让我们将所有这些组合到一个 sn-p 中:

setTimeout(function() {
    var counter = 0;
    var interval = setInterval(function(){
         document.getElementById("button-id").click(); // Clicks the button
         counter++; // Increases counter after every click
         if(counter == 10) clearInterval(interval); // Stops after 10 clicks
    },1000); // Will click the button every second
}, 10000) // Starts after 10 seconds

希望对你有帮助:)

【讨论】:

    【解决方案2】:
    function reClick(){
        for(var i=0;i<10;i++){
           $("#button-id").click(function(){
              alert("Clicked");
           });
        }
    

    }
    $("#button-id).click(reClick());

    【讨论】:

    • 欢迎来到 StackOverflow,请添加一些解释,比如这段代码是如何工作的以及它是如何解决问题的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多