简体   繁体   English

单击一个元素并将其从数组中删除

[英]Click on an element and delete it from an array

itemElement.onclick=function(){
        //remove element from view
        this.parentNode.removeChild(this);

        //find the element that was clicked on in the array
        //which should be itemElement(aka this)????
        var index=itemArray.indexOf(this);

        //remove it from the array
        itemArray.splice(index,1);

        //console.log to check
        console.log(itemArray);
    }

That is my code I am currently using to delete items from a list. 那是我目前用于从列表中删除项目的代码。 I want the user to be able to click on the item in their list they want to delete, delete it from the view and then delete it from an array of items. 我希望用户能够单击要删除的列表中的项目,将其从视图中删除,然后从项目数组中删除。 Deleting the element from the view is working fine, but for some reason its only deleting the last element added to the array, not the actual element that was clicked on. 从视图中删除该元素可以正常工作,但是由于某种原因,它仅删除添加到数组中的最后一个元素,而不是单击的实际元素。 So if the user makes a list that has Bob,Tom,Rob and then clicks on Tom, Tom is no longer displayed in the list but Rob will be deleted from the array. 因此,如果用户创建一个包含Bob,Tom,Rob的列表,然后单击Tom,则Tom将不再显示在列表中,但Rob将被从阵列中删除。

Here is that whole block of code 这是整个代码块

const addButton =<HTMLButtonElement>document.querySelector("#addButton");
const saveButton = document.querySelector("#saveButton");
const itemsSection = document.querySelector("#itemsSection");
const itemArray: Array<string> = [];

let itemElement;
let newItem: string;
let itemText: string;

//add new item to shopping list
addButton.onclick=function addItem(){

    //add each item to the list
    itemElement = document.createElement("li");
    newItem = (<HTMLInputElement>document.querySelector("#newItem")).value;
    itemText = document.createTextNode(newItem);
    itemElement.appendChild(itemText);
    itemsSection.appendChild(itemElement);
    document.querySelector("#newItem").value="";

    //push each item to our array to store in local storage
    itemArray.push(newItem);
    console.log(itemArray);

    itemElement.onclick=function deleteItem(){
        var index=itemArray.indexOf(this);
        this.parentNode.removeChild(this);

        itemArray.splice(index,1);

        console.log(itemArray);
    }

}

As you can see im using typescript 如您所见,即时通讯使用打字稿

Your code is failing to find the element in the array: 您的代码无法在数组中找到元素:

index=itemArray.indexOf(this);

This is setting index to -1 because the item is not found : 因为找不到该项目,所以将index设置为-1:

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present. indexOf()方法返回可以在数组中找到给定元素的第一个索引;如果不存在,则返回-1。

When you then call 当您再致电时

itemArray.splice(index,1);

You're always removing the last element of the array, because that's how splice works when passed negative numbers : 您总是要删除数组的最后一个元素,因为这是传递负数时拼接的工作方式

start 开始

Index at which to start changing the array. 开始更改数组的索引。 If greater than the length of the array, actual starting index will be set to the length of the array. 如果大于数组的长度,则实际的起始索引将设置为数组的长度。 If negative, will begin that many elements from the end. 如果为负,则将从头开始那么多元素。

You need to debug why itemArray.indexOf(this); 您需要调试为什么itemArray.indexOf(this); is not finding the item (and we need to see more code to help you with that) and you should say: 找不到商品(我们需要查看更多代码来帮助您),您应该说:

if(index >=0)
    itemArray.splice(index,1);
else
    console.log("Could not find index"); // or better error handling ;-)

Try changing: 尝试更改:

//push each item to our array to store in local storage
itemArray.push(newItem);

to: 至:

//push each item to our array to store in local storage
itemArray.push(itemElement);

If that doesn't work, try adding an attribute to itemElement containing the index of the element in itemArray , so that you can know which element to remove without having to rely on indexof() and DOM objects. 如果还是不行,请尝试添加属性itemElement包含的元素的索引itemArray ,这样就可以知道,而不必依赖于删除哪一个元素indexof()和DOM对象。 Something like this totally untested version of your code: 像这样的完全未经测试的代码版本:

const addButton =<HTMLButtonElement>document.querySelector("#addButton");
const saveButton = document.querySelector("#saveButton");
const itemsSection = document.querySelector("#itemsSection");
const itemArray: Array<string> = [];

let itemElement;
let newItem: string;
let itemText: string;

//add new item to shopping list
addButton.onclick=function addItem(){

    //add each item to the list
    itemElement = document.createElement("li");
    newItem = (<HTMLInputElement>document.querySelector("#newItem")).value;
    itemText = document.createTextNode(newItem);
    itemElement.appendChild(itemText);
    itemsSection.appendChild(itemElement);
    document.querySelector("#newItem").value="";

    // ***here** store the index of the element:
    itemElement.id = "itemIndex_" + (itemArray.push(newItem) -1);
    console.log(itemArray);

    itemElement.onclick=function deleteItem(){
        // get my index from my ID rather than indexOf:
        var index=parseInt(this.id.split('_')[1]);
        this.parentNode.removeChild(this);

        itemArray.splice(index,1);

        console.log(itemArray);
    }

}

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

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