简体   繁体   English

单击其他按钮后按钮事件侦听器未执行

[英]Button eventlistener not executing after other button clicked

https://codepen.io/ardiandaffa/pen/yLOZGGa https://codepen.io/ardiandaffa/pen/yLOZGGa

the code :编码 :

 function addProjectInit() { const modal = document.getElementById('modal'); const addProjectButton = document.getElementById('add-btn'); const span = document.getElementsByClassName('close')[0]; addProjectButton.onclick = () => { modal.style.display = "block"; } span.onclick = () => modal.style.display = "none"; window.onclick = () => { if (event.target == modal) { modal.style.display = "none"; } } }; function addProject() { const projectInputButton = document.getElementById('add-btn-modal'); const labelProject = document.getElementById('project-label'); const modal = document.getElementById('modal'); projectInputButton.onclick = () => { let title = document.getElementById('project-title-input').value; labelProject.innerHTML += createProject(title); modal.style.display = "none"; }; } function createProject (project) { return `<option value="Default">${project}</option>` } function addTodo() { const todoTitle = document.getElementById('todo'); const todoDate = document.getElementById('date'); const todoPriority = document.getElementById('todo-priority'); const todoButton = document.getElementById('todo-button'); const todoBox = document.getElementById('todolist-box'); todoButton.addEventListener("click", () => { let todoObject = todoObj(todoTitle.value, todoDate.value, todoPriority.options[todoPriority.selectedIndex].value); todoBox.innerHTML += createTodolist(todoObject); }); }; const todoObj = (title, dueDate, priority) => { return {title, dueDate, priority}; }; function createTodolist (todoObject) { return `<div class="todo-table"> <input type="checkbox" class="checkbox-todolist"> <a class="title-todolist">${todoObject.title}</a> <a class="date-todolist">${todoObject.dueDate}</a> <a class="priority-todolist">${todoObject.priority}</a> </div>` }; addProjectInit(); addProject(); addTodo();

So in the codepen above I could click the "ADD PROJECT" button.所以在上面的代码笔中,我可以点击“添加项目”按钮。 but, after adding todo and clicking the "ADD" button on the right side the "ADD PROJECT" button didn't work and the modal didn't show.但是,在添加待办事项并单击右侧的“添加”按钮后,“添加项目”按钮不起作用并且模态没有显示。 Why is that?这是为什么?

You are appending html directly and this reset all registered events您正在直接附加 html,这会重置所有已注册的事件

 const todoBox = document.getElementById('todolist-box')
 todoBox.innerHTML += createTodolist(todoObject);

The add project button is inside todoBox so the onClick event reset whenever you change content of the dom添加项目按钮位于 todoBox 内,因此每当您更改 dom 的内容时,onClick 事件都会重置

Create another div around and use this instead to append html在周围创建另一个 div 并使用它来附加 html

Like this https://codepen.io/stackoverflowuser/pen/xxVMMqy像这样https://codepen.io/stackoverflowuser/pen/xxVMMqy

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

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