简体   繁体   English

如何定位 <li> 列表之间的元素和转移

[英]How to Target <li> Element and Transfer between Lists

I am a newbie here. 我是新手。 I have two different lists in HTML: the first list contains several elements, the second list contains none. 我在HTML中有两个不同的列表:第一个列表包含几个元素,第二个列表包含无。

My goal is to transfer the elements of list 1 to list 2. I do want to transfer those element clicking on the "move" symbol (which I positioned). 我的目标是将列表1的元素传输到列表2.我想转移那些元素,点击“移动”符号(我定位)。

However, I do not know how to target the li element to transfer. 但是,我不知道如何将li元素作为目标进行转移。

I do know it is a basic question, but still thanks for your support and time ! 我知道这是一个基本问题,但仍然感谢您的支持和时间!

I have tried to addEventListener to "move". 我试图addEventListener“移动”。 Therefore, I have tried to use textContext to modify the list 2 elements. 因此,我尝试使用textContext来修改列表2元素。 However, I do not know how to target the li element in the first list. 但是,我不知道如何在第一个列表中定位li元素。

//FIRST LIST
<ul class="list-group todos mx-auto text-light">
          <li class="list-group-item d-flex justify-content-between align-items-center">
            <span>play mariokart</span>
            <i class="far fa-trash-alt move"></i>
          </li>
          <li class="list-group-item d-flex justify-content-between align-items-center">
            <span>defeat ganon in zelda</span>
            <i class="far fa-trash-alt move"></i>
          </li>

//SECOND LIST
<h1>Items in the Pack</h1>
      <ul class ="pack">
        <li> </li>
        <li> </li>
        <li> </li>
      </ul>

 const list = document.querySelector('.todos');
 const pack = document.querySelector(".pack");


 list.addEventListener('click', e => {

      if(e.target.classList.contains('move')){
       pack.querySelector('li').textContent = `${//missing variable//}%`; 

      }

    }); ```

first of all you need to close off your first list with /ul . 首先,您需要使用/ ul关闭第一个列表。

put the move class in the li element that contains your todos 将move类放在包含你的待办事项的li元素中

<li class="move list-group-item d-flex justify-content-between align-items-center">

then, this script should handle everything. 那么,这个脚本应该处理一切。

<script>
    window.onload = function() {
        document.getElementById('movebutton').addEventListener('click', function() {
            const list = document.querySelector('.todos').getElementsByTagName('li');
            const pack = document.querySelector('.pack');
            for(var x=0; x<list.length; x++) {
                if(list[x].classList.contains('move')) {
                    pack.appendChild(list[x]);
                    x--;
                }
            }
        });
    }
</script>

appendChild automatically removes from the original list, so you need x-- to correct where the next loop points to. appendChild会自动从原始列表中删除,因此您需要x--来更正下一个循环所指向的位置。

and a button that attaches to the event listener. 和一个附加到事件监听器的按钮。

<button id="move">move</button>

Edit: 编辑:

Apologies, i missed the part where you wanted to move them individually. 道歉,我错过了你想要单独移动它们的部分。 The following fixes that and also makes sure your list elements maintain their class and styles etc. Click on the move text to move them. 修复了以下内容并确保列表元素保持其类和样式等。单击移动文本以移动它们。

Also, just added a return back to the original list feature. 此外,只需添加返回原始列表功能。

 window.onload = function() { const list = document.querySelector('.todos'); const pack = document.querySelector('.pack'); list.addEventListener('click', function(e) { if(e.target.classList.contains('move')) { e.target.innerText = " - return"; li = e.target.parentElement; pack.appendChild(li); } }); pack.addEventListener('click', function(e) { if(e.target.classList.contains('move')) { e.target.innerText = " - move"; li = e.target.parentElement; list.appendChild(li); } }); } 
 //FIRST LIST <ul id="first" class="list-group todos mx-auto text-light"> <li class="list-group-item d-flex justify-content-between align-items-center"> <span style="font-weight:bold">play mariokart</span> <i class="far fa-trash-alt move"> - move</i> </li> <li class="list-group-item move d-flex justify-content-between align-items-center"> <span>defeat ganon in zelda</span> <i class="far fa-trash-alt move"> - move</i> </li> </ul> //SECOND LIST <h1>Items in the Pack</h1> <ul id="second" class ="pack"> <li> </li> <li> </li> <li> </li> </ul> 

You can use parentElement to get the list element which is the immediate parent of the element with the move class. 您可以使用parentElement来获取list元素,该元素是具有move类的元素的直接父元素。

You can then create new list elements and append them to the destination list. 然后,您可以创建新的列表元素并将它们附加到目标列表。 Here's the flow, with minor changes to your code. 这是流程,对代码进行了少量更改。

 const list = document.querySelector('.todos');
 const pack = document.querySelector(".pack");

 list.addEventListener('click', e => {
   if (e.target.classList.contains('move')) {
     let originalListEntry = e.target.parentElement;
     var newListEntry = document.createElement("li")
     newListEntry.innerText = originalListEntry.innerText
     pack.appendChild(newListEntry)
   }
 });

Here's a working example I've added a + inside the move span for demo purposes. 这是一个工作示例我在移动范围内添加了一个+用于演示目的。

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

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