我正在创建一个程序,其中包含购物 list 的物品,然后用户可以添加物品到 list 中.我希望用户能够通过简单地点击该项目从购物 list 中删除项目.当我点击该项目时,无论我点击哪个项目,最后一个项目都会被删除.我只是想找出为什么只删除数组中的最后一项,而不是我正在 Select 的项.

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Shopping List</title>

    <!-- Link Google Font -->
    <link href="https://fonts.googleapis.com/css2?family=Nunito&display=swap" rel="stylesheet">
    <!-- External CSS link-->
    <link rel="stylesheet" href="./css/styles.css">
</head>

<body>
    <div class="container">
        <h2>Shopping List</h2>

        <div class="header">
            <input type="text" id="input" placeholder="Item">
            <span onclick="updateList(myArray)" id="addBtn"><button>Add Item</button></span>
        </div>
        <span value="\uOOD7" class="close" onclick="deleteItem(myArray)">
        <ul id="itemList">
            

            
        </ul>
    </span>
    </div>

    <script src="mainForTask2.js"></script>
</body>

</html>



Javascript

    //This is a javascript program which added the items in my array to an unordered list
    let myArray = ["Sugar", "Milk", "Bread", "Apples"];
    let list1 = document.querySelector("#itemList");

    //This function pushed my array items to create the list
    arrayList = (arr) => {
    let items = arr.forEach(item => {
    let li = document.createElement('li');
    li.textContent = item;
    list1.appendChild(li)
    });
    }

    arrayList(myArray)


    //This function changed the background color of two of the list items to show that they are sold
    const idSelector = () => {
    let idElement = document.getElementsByTagName("li")
    idElement[0].style.color = "red"
    idElement[3].style.color = "red"
    console.log(idElement.value)
    }

    idSelector()

    //This function uses the user input from the form to add items to the list
    updateList = (arr) => {
    let blue = document.getElementById("input").value;

    if (blue === "") {
    alert("Please enter a value if you wish to add something to your 
    list.")
    } else {
    arr.push(blue);
    list1.innerHTML = '';
    arrayList(myArray)
    idSelector()
    }

    }

    //This function is meant to delete the specified item chosen by the user from the shopping list and the array
    deleteItem = (arr) => {
    let red = document.getElementById("close");
    const index = arr.indexOf(red);
    const x = arr.splice(index, 1)
    console.log(arr)
    console.log(x)
    list1.innerHTML = '';
    arrayList(myArray)
    idSelector()
    }







推荐答案

在这里,我们首先需要找到被点击的元素,然后找到它的索引,然后从数组中删除特定的元素.

let myArray = ["Sugar", "Milk", "Bread", "Apples"];
let list1 = document.querySelector("#itemList");

//This function pushed my array items to create the list
arrayList = (arr) => {
  let items = arr.forEach(item => {
    let li = document.createElement('li');
    li.textContent = item;
    list1.appendChild(li)
  });
}

arrayList(myArray)


//This function changed the background color of two of the list items to show that they are sold
const idSelector = () => {
  let idElement = document.getElementsByTagName("li")
  idElement[0].style.color = "red"
  idElement[3].style.color = "red"
}

idSelector()

//This function uses the user input from the form to add items to the list
updateList = (arr) => {
  let blue = document.getElementById("input").value;

  if (blue === "") {
    alert("Please enter a value if you wish to add something to your list.")
  } else {
    arr.push(blue);
    list1.innerHTML = '';
    arrayList(myArray)
    idSelector()
  }

}

//This function is meant to delete the specified item chosen by the user from the shopping list and the array
deleteItem = (event) => {
  let clk = event.target.innerHTML;
  console.log(clk);
  let index = myArray.indexOf(clk);
  if (index > -1) 
  {
    myArray.splice(index, 1);
  }
  list1.innerHTML = '';
  arrayList(myArray)
}
<body>
    <div class="container">
        <h2>Shopping List</h2>

        <div class="header">
            <input type="text" id="input" placeholder="Item">
            <span onclick="updateList(myArray)" id="addBtn"><button>Add Item</button></span>
        </div>
        <span value="\uOOD7" class="close" onclick="deleteItem(event)">
        <ul id="itemList">
            

            
        </ul>
    </span>
    </div>

    <script src="mainForTask2.js"></script>
</body>

Javascript相关问答推荐

在Chart.js 4.4.2中移动到不同大小的容器时,图表不会调整大小

如何在JavaScript中通过一次单击即可举办多个活动

如何将拖放功能添加到我已自定义为图像的文件输入HTML标签中?

如何使用JavaScript用等效的功能性HTML替换标记URL格式?

D3多线图显示1线而不是3线

如何使用侧边滚动按钮具体滚动每4个格?

django无法解析余数:[0] from carray[0]'

按下同意按钮与 puppeteer 师

分层树视图

html + java script!需要帮助来了解为什么我得到(无效的用户名或密码)

如何从隐藏/显示中删除其中一个点击?

Ember.js 5.4更新会话存储时如何更新组件变量

编剧如何获得一个div内的所有链接,然后判断每个链接是否都会得到200?

Angular 中的类型错误上不存在获取属性

我创建了一个创建对象的函数,我希望从该函数创建的对象具有唯一的键.我怎么能做到这一点?

Angular 形式,从DOM中删除不会删除指定索引处的内容,但会删除最后一项

无法读取未定义的属性(正在读取合并)-react RTK

构建器模式与参数对象输入

Next.js无法从外部本地主机获取图像

使用createBrowserRoutVS BrowserRouter的Reaction路由