You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tampermonkey脚本中AutoIT IsArray等效实现及元素点击问题求助

JavaScript/jQuery 数组判断与批量元素点击解决方案

Hey there! Let's tackle your two main questions first, then fix that click issue you're running into.

1. 对应 AutoIT If is array then 的数组判断方法

在 JavaScript 和 jQuery 里,都有直接判断数组的方法,完全能替代 AutoIT 的数组判断逻辑:

  • 原生 JavaScript:用 Array.isArray(yourVariable),返回布尔值。比如:
    let testArr = [1, 2, 3];
    if (Array.isArray(testArr)) {
      // 是数组时执行的逻辑
    }
    
  • jQuery:用 $.isArray(yourVariable),用法和原生类似:
    let testArr = [1, 2, 3];
    if ($.isArray(testArr)) {
      // 是数组时执行的逻辑
    }
    

2. 解决批量点击 .center 元素的问题

你之前的代码只会点击第一个元素,是因为每次都用了 [0] 取数组的第一个项。另外你代码里的 $1 应该是笔误,应该是 setTimeout 对吧?

方案一:原生 JavaScript 实现批量点击

如果要点击所有带 class="center" 的元素,可以遍历获取到的元素集合:

setTimeout(function () {
  // 获取所有匹配的元素
  const centerElements = document.getElementsByClassName("center");
  // 遍历并逐个点击
  for (let elem of centerElements) {
    elem.click();
  }
}, 999);

或者用更现代的 querySelectorAll:

setTimeout(function () {
  document.querySelectorAll(".center").forEach(elem => {
    elem.click();
  });
}, 999);

方案二:jQuery 实现批量点击

如果用 jQuery 的话,更简洁,直接用 .click() 方法就能触发所有匹配元素的点击事件:

setTimeout(function () {
  $(".center").click();
}, 999);

为什么你之前的代码无效?

  • $("center")[0] 只是判断第一个 .center 元素是否存在,但后续点击还是只操作了第一个元素;
  • $1(function(){...}, 999) 是语法错误,应该是 setTimeout,这也会导致代码无法正常执行。

内容的提问来源于stack exchange,提问作者Mammamio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:28:27