简体   繁体   English

使用 addEventlistener 修改对象

[英]modify an object with a addEventlistener

I have an array of objects which looks like this:我有一个看起来像这样的对象数组:

 0: {name: "a", id: 0, trash: false} 1: {name: "b", id: 1, trash: false} 2: {name: "c", id: 2, trash: false} 3: {name: "d", id: 3, trash: false} 4: {name: "e", id: 4, trash: false} 5: {name: "f", id: 5, trash: false}

and I wrote this code to change the value trash from true to false everytime I click.我编写了这段代码,每次单击时将trash值从true更改为false

 list.addEventListener("click", (e) => { if (e.target.classList.contains("delete")) { e.target.parentElement.remove(); console.log((listTodo[2].trash = true)); console.log(listTodo); } });

I know that writing in this way i's only going to chance the value of the specific object(in this case the object in the position 2).我知道以这种方式写作我只会碰巧特定对象的值(在这种情况下是位置 2 的对象)。

So, what I'm trying to do is dig into the clicked object and change the value.所以,我想要做的是深入点击对象并更改值。

here is a snippet of the HTML part of the code to give an idea.这是代码的 HTML 部分的片段,以提供一个想法。

 <li class="list-group-item d-flex justify-content-between align-items-center"> <span>lorem1234</span><i class="far fa-trash-alt delete" job="delete" id=5></i> </li>

I would suggest you use data-* attributes which are easy to read with getAttribute (or data() if you happen to use jQuery).我建议您使用data-*属性,这些属性很容易通过getAttribute (或data()如果您碰巧使用 jQuery)读取。

Then you can just read the job and id easily enough and convert the latter to an index of the array of listTodo .然后你可以很容易地读取jobid并将后者转换为listTodo数组的listTodo

 var listTodo = [{name: "a", id: 0, trash: false}, {name: "b", id: 1, trash: false}, {name: "c", id: 2, trash: false}]; var list = document.querySelector("#my-list"); list.addEventListener("click", (e) => { if (e.target.getAttribute("data-job") == "delete") { e.target.parentElement.remove(); var idx = parseInt(e.target.getAttribute("data-id"),10); console.log((listTodo[idx].trash = true)); console.log(listTodo); } });
 <script src="https://kit.fontawesome.com/a076d05399.js"></script> <ul id="my-list"> <li class="list-group-item d-flex justify-content-between align-items-center"> <span>lorem1</span><i class="far fa-trash-alt delete" data-job="delete" data-id="0"></i> </li> <li class="list-group-item d-flex justify-content-between align-items-center"> <span>lorem2</span><i class="far fa-trash-alt delete" data-job="delete" data-id="1"></i> </li> <li class="list-group-item d-flex justify-content-between align-items-center"> <span>lorem3</span><i class="far fa-trash-alt delete" data-job="delete" data-id="2"></i> </li> </ul>

You should use data attribute data-job as others have said, then you can do this with 3 steps in your javascript click handler.您应该像其他人所说的那样使用数据属性data-job ,然后您可以在 javascript 单击处理程序中通过 3 个步骤来完成此操作。

  • 1: check the clicked element has has the data-job="delete" . 1:检查被点击的元素是否有data-job="delete" This is easy to do using the dataset attribute if (e.target.dataset.job == "delete")这很容易使用dataset属性if (e.target.dataset.job == "delete")
  • 2: Remove it from the DOM e.target.parentElement.remove(); 2:从DOM中移除e.target.parentElement.remove();
  • 3: Change the "trash" value in your array. 3:更改数组中的“垃圾”值。 You get the id of the clicked element e.target.id and use it as the array key: listTodo[e.target.id].trash = true;你得到被点击元素的id e.target.id并将其用作数组键: listTodo[e.target.id].trash = true;

That's all.就这样。 The javascript function is next, then a snippet below that showing it working.接下来是 javascript 函数,然后是下面显示它工作的片段。

var list = document.getElementById("list");
list.addEventListener("click", (e) => {

    // Check if the clicked element has the data-job="delete"
    if (e.target && e.target.dataset.job == "delete") {

        // Remove the element from the DOM
        e.target.parentElement.remove();

        // set trash to true for this element in the array using this id as the array key
        listTodo[e.target.id].trash = true;
  }
});

 var listTodo = { 0: {name: "a", id: 0, trash: false}, 1: {name: "b", id: 1, trash: false}, 2: {name: "c", id: 2, trash: false}, 3: {name: "d", id: 3, trash: false}, 4: {name: "e", id: 4, trash: false}, 5: {name: "f", id: 5, trash: false} }; var list = document.getElementById("list"); list.addEventListener("click", (e) => { // check if the clicked element has the data-job="delete" if (e.target && e.target.dataset.job == "delete") { // Remove the clicked element from the DOM e.target.parentElement.remove(); // Set trash=true for the clicked element using the id as the array key listTodo[e.target.id].trash = true; console.log ("Deleting element "+e.target.id); } });
 i { display: block; height: 20px; width: 20px; margin-left: 10px;}
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.1/css/all.min.css" rel="stylesheet" /> <ul id="list"> <li class="list-group-item d-flex justify-content-between align-items-center"> <span>0</span><i class="far fa-trash-alt delete" data-job="delete" id="0"></i> </li> <li class="list-group-item d-flex justify-content-between align-items-center"> <span>1</span><i class="far fa-trash-alt delete" data-job="delete" id="1"></i> </li> <li class="list-group-item d-flex justify-content-between align-items-center"> <span>2</span><i class="far fa-trash-alt delete" data-job="delete" id="2"></i> </li> <li class="list-group-item d-flex justify-content-between align-items-center"> <span>3</span><i class="far fa-trash-alt delete" data-job="delete" id="3"></i> </li> <li class="list-group-item d-flex justify-content-between align-items-center"> <span>4</span><i class="far fa-trash-alt delete" data-job="delete" id="4"></i> </li> <li class="list-group-item d-flex justify-content-between align-items-center"> <span>5</span><i class="far fa-trash-alt delete" data-job="delete" id="5"></i> </li> </ul>

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

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