简体   繁体   English

如何使用 JS 中的 Onclick 将元素从一侧移动到另一侧?

[英]How To Move Elements From One Side To Another Using Onclick in JS?

I have a to do list program where the user enters an item that gets added onto a list.我有一个待办事项列表程序,用户可以在其中输入一个添加到列表中的项目。 The problem is that when I click on the items, the items stay inside of the To Do list rather than being moved to the Done list.问题是当我点击这些项目时,这些项目会留在待办事项列表中,而不是被移动到完成列表中。 What I'm trying to do is move my elements from my to do list to my done list after the user clicks on the items which checks the items as completed.我想要做的是在用户单击检查项目已完成的项目后,将我的元素从我的待办事项列表移至我的已完成列表。 I am wondering how to receive the desired results with this program which is to move clicked elements from the to do list to the done list.我想知道如何使用此程序接收所需的结果,该程序将单击的元素从待办事项列表移动到已完成列表。 Here is the code that you can test out yourself: https://jsfiddle.net/g60dx2e1/以下是您可以自己测试的代码: https : //jsfiddle.net/g60dx2e1/

 // Create a "close" button and append it to each list item var myNodelist = document.getElementsByTagName("LI"); var i; for (i = 0; i < myNodelist.length; i++) { var span = document.createElement("SPAN"); var txt = document.createTextNode("\×"); span.className = "close"; span.appendChild(txt); myNodelist[i].appendChild(span); } // Click on a close button to hide the current list item var close = document.getElementsByClassName("close"); var i; for (i = 0; i < close.length; i++) { close[i].onclick = function() { var div = this.parentElement; div.style.display = "none"; } } // Add a "checked" symbol when clicking on a list item var list = document.querySelector('ul'); list.addEventListener('click', function(ev) { if (ev.target.tagName === 'LI') { ev.target.classList.toggle('checked'); } }, false); // Create a new list item when clicking on the "Add" button function newElement() { var li = document.createElement("li"); var inputValue = document.getElementById("myInput").value; var t = document.createTextNode(inputValue); li.appendChild(t); if (inputValue === '') { alert("You must write something!"); } else { document.getElementById("myUL").appendChild(li); } document.getElementById("myInput").value = ""; var span = document.createElement("SPAN"); var txt = document.createTextNode("\×"); span.className = "close"; span.appendChild(txt); li.appendChild(span); for (i = 0; i < close.length; i++) { close[i].onclick = function() { var div = this.parentElement; div.style.display = "none"; } } } function newFunction() { var todos = document.getElementById("myUL"); if (todos) { todos.innerHTML = ''; } }
 <form id="todo" style="float:left"> <h1 id="todo" style="font-family:Helvetica; color:#006600"><b>To Do</b> <input type="text" id="myInput" placeholder="Add Item"> <span onclick="newElement()" class="addBtn">Add</span> <input type="button" onclick="newFunction()" value="Clear"> <ul id="myUL"> </ul> </h1> </form> <form id="done" style="float:right"> <h2 id="done" style="font-family:Helvetica; color:#006600"><b>Done</b> <input type="button" onclick="NewFunction()" value="Clear"></h2> <ul id="myUL"> </ul> </form>

Add a click event on the list that removes the element and moves it to the other list:在列表上添加一个单击事件以删除元素并将其移动到另一个列表:

document.getElementById("myUL").onclick = (e) => {
  document.getElementById("myUL").removeChild(e.srcElement);
  document.getElementById("myUL2").appendChild(e.srcElement);
};

Note: you have both lists with id="myUL" .注意:您有id="myUL"两个列表。 You should make them different as I have in the code.您应该使它们与我在代码中的不同。

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

相关问题 如何将元素从一个列表移动到另一个列表 - How to move elements from one list to another 使用Immutable.js将元素从一个列表移动到另一个列表 - Move elements from one List to another with Immutable.js 如何在不重置为原始 position 的情况下将元素从一个 div 移动到另一个? (interact.js) - How to move elements from one div to another without resetting to original position? (interact.js) 如何使用 hash 将元素从一个数组伪随机移动到另一个数组? - How can I move elements from one array to another pseudo-randomly using a hash? 如何在 React JS 中将 props onClick 从一个组件传递到另一个组件 - How to pass props onClick from one component to another in React JS 如何使用 JS 将一个元素的 ID 移动到另一个元素 - How to move the ID of one element to another using JS 如何在垂直滚动条上使用Stellar.js从侧面移动/动画元素? - How to move/animate elements in from the side with Stellar.js on a vertical scroll? 如何使用JS / onclick过滤Wordpress中的元素? - How to filter elements in Wordpress Using JS / onclick? 如何使用js或jquery使小图像从屏幕的一侧移动到另一侧? - How to make a small image move from one side of the screen to the other with js or jquery? 如何使用Enter从一个输入框移动到具有相同类的另一个输入框 - How to move from one inputbox to another with the same class using enter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM