简体   繁体   English

无法在 javascript 中的字体真棒图标上触发单击事件侦听器

[英]can't fire click eventListener on font awesome icons in javascript

I have been trying to make a simple todo list in which i have added font awesome icons in front of every list item, but when i click on that icon it doesn't gets removed from the list.... Here's my js code:我一直在尝试制作一个简单的待办事项列表,其中我在每个列表项的前面添加了很棒的字体图标,但是当我单击该图标时,它不会从列表中删除....这是我的 js 代码:

   const list = document.querySelector('.list');

   list.addEventListener('click', e => {
    if (e.target.className === fa-solid') {
        let li = e.target.parentNode;
        li.parentNode.removeChild(li);
    }
})

here fa-solid is the class of the font awesome icon and when i click on this icon my code doesn't run.这里fa-solid是字体真棒图标的 class,当我点击这个图标时,我的代码没有运行。

I got the solution for my question.我得到了我的问题的解决方案。 The only thing that was wrong in my code was the name of the class of font awesome icon.我的代码中唯一错误的是字体真棒图标 class 的名称。 We have to pass the entire class name in the code and not just fa-solid .我们必须在代码中传递整个 class 名称,而不仅仅是fa-solid

const list = document.querySelector('.list');

list.addEventListener('click', e => {
    if (e.target.className === 'fa-solid fa-trash-can') {
        let li = e.target.parentNode;
        li.parentNode.removeChild(li);
    }
})

That's it.就是这样。

you messing ' in the if statement 'fa-solid'你在 if 语句 'fa-solid' 中弄乱了 '

 const list = document.querySelector('.list'); list.addEventListener('click', e => { if (e.target.className === 'fa-solid') { let li = e.target.parentNode; li.parentNode.removeChild(li); } })

There is a quote missing in front of fa-solid inside the if if 中的 fa-solid 前面缺少引号

if (e.target.className === 'fa-solid')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM