如何使用jquery将事件侦听器添加到具有相同类的每个元素



我不明白为什么我的代码不工作。我正在工作时,点击弹出模式。什么才是正确的方法?谢谢。

<div class="projects-container">
<a class="project">
<div class="img-wrapper">
<img src="./images/background-project-filler.png" alt="">
</div>
<p class = 'project-title'>Lorem Ipsum</p>
</a>
<a class="project">
<div class="img-wrapper">
<img src="./images/background-project-filler.png" alt="">
</div>
<p class = 'project-title'>Lorem Ipsum</p>
</a>
<a class="project">
<div class="img-wrapper">
<img src="./images/background-project-filler.png" alt="">
</div>
<p class = 'project-title'>Lorem Ipsum</p>
</a>
<a class="project">
<div class="img-wrapper">
<img src="./images/background-project-filler.png" alt="">
</div>
<p class = 'project-title'>Lorem Ipsum</p>
</a>
</div>

Jquery

$('.project').each( (i) => {
$('project')[i].click( () => {
console.log(`Project ${i}`);

});
})

您在each函数中漏掉了点:将project更改为.project

相关内容

  • 没有找到相关文章