Javascript 向每个数组项添加一次单击

Javascript 向每个数组项添加一次单击,javascript,jquery,tampermonkey,Javascript,Jquery,Tampermonkey,因此,我的问题是,我希望脚本在简单地将每个类登录到控制台后单击它 我尝试在几段代码之后添加一些.click()方法,但运气不好 下面是代码 var array = ['.button.color.color-41d841', '.button.color.color-dc0000', '.button.color.color-1e00e9', '.button.color.color-ff6f00']; var interval = 1000; // how much time should t

因此,我的问题是,我希望脚本在简单地将每个类登录到控制台后单击它

我尝试在几段代码之后添加一些.click()方法,但运气不好

下面是代码

var array = ['.button.color.color-41d841', '.button.color.color-dc0000', '.button.color.color-1e00e9', '.button.color.color-ff6f00'];
var interval = 1000; // how much time should the delay between two iterations be (in milliseconds)?
array.forEach(function(el, index) {
  setTimeout(function() {
    console.log(el);
  }, index * interval);
});
console.log('Loop finished.');
它只是打印元素的类

已解决

var array = ['.button.color.color-41d841', '.button.color.color-dc0000', '.button.color.color-1e00e9', '.button.color.color-ff6f00'];
var interval = 1000; // how much time should the delay between two iterations be (in milliseconds)?
array.forEach(function(el, index) {
  setTimeout(function() {
    console.log(el);
    $(el).click()
  }, index * interval);
});
console.log('Loop finished.');

您需要调用下面的脚本

$(document).on('click',el,function(){})

因为您正在动态绑定单击事件

 var array = ['.label-key', '.button.color.color-dc0000', '.button.color.color-1e00e9', '.button.color.color-ff6f00'];
var interval = 1000; // how much time should the delay between two iterations be (in milliseconds)?
array.forEach(function (el, index) {
$(document).on('click',el,function(){}); 
setTimeout(function () {
console.log(el);
}, index * interval);
});
console.log('Loop finished.');

试试这个,你需要找到元素并点击它

var array = ['.button.color.color-41d841', '.button.color.color-dc0000', '.button.color.color-1e00e9', '.button.color.color-ff6f00'];
var interval = 1000; // how much time should the delay between two iterations be (in milliseconds)?
array.forEach(function (el, index) {
setTimeout(function () {
console.log(el);
document.querySelector(el).click()
}, index * interval);
});

如果按钮的类是相同的名称怎么办。因此有多个按钮具有此类名。我不能使用类似“.button.color[0]”的名称吗