简体   繁体   English

将点击事件附加到尚未创建的DOM元素(没有jQuery)

[英]Attaching click events to DOM elements that are not yet created (no jQuery)

So I am trying to create a click event for DOM elements that have not yet been created. 因此,我试图为尚未创建的DOM元素创建click事件。 In the course I am taking, we made the todo list with jQuery, but I need more practice with reuglar JS so I am recreating it. 在我上课的过程中,我们使用jQuery创建了待办事项列表,但是我需要使用reuglar JS进行更多练习,因此我将重新创建它。 The last block of code I am assuming is where it will go, but I posted most of my code just in case I am wrong. 我假定的最后一块代码是它将要去的地方,但是我发布了大部分代码,以防万一我错了。

var h1 = document.querySelector("h1")
var lis = document.querySelectorAll("li")
var trashes = document.querySelectorAll("span")
var filterInput = document.querySelector("input")
var container = document.querySelector("#container")
var ul = document.querySelector("ul")

//forEach loop
lis.forEach(function(li){
li.addEventListener("click", function(){
    this.classList.toggle("completed")
    console.log(this.parentElement)
})
trashes.forEach(function(trash){
trash.addEventListener("click", function(){
    trash.parentElement.remove()
    });
});
})

//I am assuming the click event would go in this function? 
filterInput.addEventListener("keypress", function(event){
  if(event.which === 13){
   var todo = filterInput.value;
   var newLi = document.createElement("li")
   var element = newLi.appendChild(document.createTextNode(todo));
    ul.appendChild(newLi);
    filterInput.value = " ";
  }
});

HTML 的HTML

<div id="container">
<h1> To-do List </h1>
<input type="text" placeholder="Add New Todo">
<ul>
   <li><span><i class="far fa-trash-alt"></i>
      </span> Walk dog </li>
  <li><span><i class="far fa-trash-alt"></i>
      </span> Buy  undershirts </li>
  <li><span><i class="far fa-trash-alt"></i>
       </span> Study </li>

CSS 的CSS

.completed {
  text-decoration: line-through;
  opacity: .4;
}

The closest I have come is 我最接近的是

filterInput.addEventListener("keypress", function(event){
  if(event.which === 13){
   var todo = filterInput.value;
   var newLi = document.createElement("li")
   var element = newLi.appendChild(document.createTextNode(todo));
    ul.appendChild(newLi);
    newLi.classList.toggle("completed")  **
    filterInput.value = " ";
  }
});

** indicates the new line I added in - newLi.classList.toggle("completed") - which toggles the class upon the newLi being created. **表示我添加的新行-newLi.classList.toggle(“ completed”)-在创建newLi时切换类。 My problem is, I want the class to toggle when clicked, like the rest of the list items, on the newLi created. 我的问题是,我希望类在单击时切换,就像在创建的newLi上的其余列表项一样。

A simpler solution (not involving delegation ), would be 一个更简单的解决方案(不涉及委托 )将是

Refactor the click handler for li 重构li的点击处理程序

lis.forEach(function(li){
li.addEventListener("click", handleLiClick);
function handleLiClick( event )
{
    var thisObj = event.currentTarget;
    thisObj.classList.toggle( "completed" )
    console.log( thisObj.parentElement );
}

And then when you create an li , bind the same click handler to it then and there 然后在创建li ,将相同的click处理程序绑定到该位置

filterInput.addEventListener("keypress", function(event){
  if(event.which === 13){
   var todo = filterInput.value;
   var newLi = document.createElement("li")
   var element = newLi.appendChild(document.createTextNode(todo));
    ul.appendChild(newLi);
    filterInput.value = " ";
    newLi.addEventListener("click", handleLiClick); //this line has been added
  }
});

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

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