如何在JavaScript中删除动态创建列表的特定部分?

huangapple go评论75阅读模式
英文:

How to delete specific parts of a dynamic created list in JavaScript?

问题

Sure, here's the translated text:

嘿,

我目前正在制作一个相当简单的购物清单,以熟悉JavaScript。

如您在图片中所见,我想在单击垃圾桶时删除其旁边的元素。
但这并不像人们想象的那样完全起作用,无论单击哪个垃圾桶,它只会删除列表中的顶部项目。

我的创建元素的函数:

function addFun(){
    // * 列表项
    const li = document.createElement('li');
    li.setAttribute('class', 'list_element');
    li.textContent = input.value;
    liste.appendChild(li);
    if(input.value !== ''){
        // * 复选框
        const checkbox = document.createElement('input');
        checkbox.setAttribute('type', 'checkbox');
        li.insertBefore(checkbox, li.childNodes[0]);
        input.value = '';
        // * 垃圾桶
        const trash = document.createElement('img');
        trash.setAttribute('src', 'https://cdn-icons-png.flaticon.com/512/3209/3209887.png');
        trash.setAttribute('class', 'image');
        li.appendChild(trash);
    }
}

我的用于删除元素的事件监听器:

document.addEventListener('click', function(e){
    if(e.target.classList.contains('image')){
        deleteIt();
    }
});

function deleteIt(){
    const li = document.querySelector('.list_element');
    li.remove();
}

我花了很长时间才使事件处理程序起作用,因为我以前从未听说过事件冒泡。
我尝试使用“this”来找到解决方案,但在上下文中我不太确定它的含义。

如果有人能仔细查看并为我提供解决方案或提示,我将不胜感激。

英文:

Shoppinglist

Hey you,

I'm currently working on a fairly simple shopping list to familiarize myself with javascript.

As you can see in the picture, I want to delete the element next to it when I click on the trashcan.
But this doesn't quite work as one would imagine and only deletes the top item from my list no matter which trashcan i click on.

My function to create the elements

function addFun(){
    // * List 
    const li = document.createElement('li');
    li.setAttribute('class', 'list_element');
    li.textContent = input.value;
    liste.appendChild(li);
    if(input.value !== ''){
        // * Checkbox
        const checkbox = document.createElement('input');
        checkbox.setAttribute('type', 'checkbox');
        li.insertBefore(checkbox, li.childNodes[0]);
        input.value = '';
        // * trashcan
        const trash = document.createElement('img');
        trash.setAttribute('src', 'https://cdn-icons-png.flaticon.com/512/3209/3209887.png');
        trash.setAttribute('class', 'image');
        li.appendChild(trash);
    }
}

My Eventlistener to remove the elements

document.addEventListener('click', function(e){
    if(e.target.classList.contains('image')){
        deleteIt();
    }
});

function deleteIt(){
    const li = document.querySelector('.list_element');
    li.remove();
}

It took me a long time to get the event handler working as I had never heard of event bubbling before.
I tried a bit with "this" to find a solution, but I wasn't quite sure what it meant in the context

I would be very happy if someone could look over it and maybe give me a solution or a tip

答案1

得分: 0

你可以通过 e.target 确定被点击的按钮。从按钮中,你可以使用 closest 找到要删除的 LI。

document.addEventListener('click', function(e){
    if(e.target.classList.contains('image')){
        deleteIt(e.target);
    }
});

function deleteIt(whichButton){
    const li = whichButton.closest("li");
    li.remove();
}
英文:

You can determine the button that was clicked by e.target. From the button you can locate the LI to delete using closest

document.addEventListener('click', function(e){
    if(e.target.classList.contains('image')){
        deleteIt(e.target);
    }
});

function deleteIt(whichButton){
    const li = whichButton.closest("li");
    li.remove();
}

答案2

得分: 0

Your deleteIt() function is not getting the li that should be deleted.

你的 deleteIt() 函数没有获取到应该被删除的 li 元素。

You should pass the target button that is clicked to the deleteIt().

你应该将被点击的目标按钮传递给 deleteIt() 函数。

In this way;

这样做:

function deleteIt(target){
函数 deleteIt(target){
const li = target.closest("li");
li.remove();
}

This way the deleteIt() knows which element to delete precisely.

这样 deleteIt() 函数就知道要精确删除哪个元素了。

I am also new to programming. Sorry if it doesn't help.

我也是编程新手。如果这没有帮助的话,很抱歉。

英文:

Your deleteIt() function is not getting the li that should be deleted.

You should pass the target button that is clicked to the deleteIt().

In this way;

    document.addEventListener('click', function(e){
    if(e.target.classList.contains('image')){
        deleteIt(e.target);
    }
});

function deleteIt(target){
    const li = target.closest("li");
    li.remove();
}

This way the deleteIt() know which element to delete pricesely.

I am also new to programming. Sorry if it doesn't help.

huangapple
  • 本文由 发表于 2023年4月19日 22:17:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定