我想用JavaScript做两次click事件,一个接一个。第一次点击代码可以正常工作,但第二次点击没有发生。第二次单击,将在0.5秒后弹出一个窗口。
我正在使用以下代码,我是JS的新手,不知道是什么问题
var x = document.getElementsByClassName('artdeco-button--secondary');
for (var i = 0; i < x.length; i++ ){
x[i].addEventListener('click',function(){
document.getElementsByClassName('ml1').click();
});
}
答案 0 :(得分:2)
您应该在dblclick
中将addEventListener
用作事件,而不是click
答案 1 :(得分:0)
您打算如何处理点击? 你在提醒什么吗?您是否要添加课程?
答案 2 :(得分:0)
// you can do the same as follows
var buttons = document.querySelectorAll('.artdeco-button--secondary');
for (const button of buttons) {
button.addEventListener('click', function() {
// but here, you are trying to apply
// an event to a list of Elements with getElementsByClassName
// if you want to execute a click event to an specific element
// you can do it as follows
document.querySelector('#elementId').click();
// or using the index of the element in an array
const target = document.querySelectorAll('.target')[0];
target.click();
// or iterating the list to apply it to each element
const targets = document.querySelectorAll('.target');
for (const target of targets) {
target.click();
}
});
}