[英]How can I get the element that is clicked on with a click handler added in a forEach?
I need to detect element count number我需要检测元素计数
I have 10 poster elements and I have a forEach
loop我有 10 个海报元素,我有一个
forEach
循环
I need the poster number that I clicked on, for example if I click on the seventh poster posternumber
should be 7
我需要我点击的海报编号,例如如果我点击第七张海报海报
posternumber
应该是7
poster.forEach(p => {
p.addEventListener('click',function(){
videoTag.src = xResult[movieLanguageUrl][/*posternumber*/];
});
});
Get the index value on the second parameter of the forEach
function and it should work:获取
forEach
function 的第二个参数的索引值,它应该可以工作:
poster.forEach((p, index) => {
p.addEventListener('click', () => {
videoTag.src = xResult[movieLanguageUrl][index + 1];
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.