我的JavaScript函数运行时没有被调用 [英] My JavaScript function runs without being called
本文介绍了我的JavaScript函数运行时没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
函数 tablePush
在单击项目时将ID推入表中.
但是,它无需点击即可运行,我也不明白为什么.
The function tablePush
pushes an id to the table upon a click on an item.
However, it runs without a click and I don't understand why.
这是我的代码:
function tablePush() {
console.log('OK');
if (pickPicture) {
console.log('TRUE');
} else {
console.log('on rentre dans le else');
console.log(this);
var idPic = this.getAttribute('id');
console.log(idPic);
table.push(idPic);
pickPicture = true;
}
}
var picture = document.getElementsByClassName('picture'),
table = [],
pickPicture = false;
for (var i = 0; i < picture.length; i++){
picture[i].addEventListener('click', tablePush());
}
推荐答案
您正在调用它.
您需要注册 tablePush
函数(而不是调用它):
You are calling it.
You need to register the function tablePush
(instead of invoking it):
picture[i].addEventListener('click', tablePush);
这篇关于我的JavaScript函数运行时没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文