简体   繁体   English

如何删除div中的最后3个div

[英]How to remove the last 3 div inside a div

removing the last 3 divs inside the div using native javascript 使用本机javascript删除div中的最后3个div

It should first look like this: 它应该首先看起来像这样:

<div id="name">
      <div>one</div>
      <div>two</div>
      <div>three</div>
      <div>four</div>
      <div>five</div>
</div>

After removing it: 删除后:

<div id="name">
      <div>one</div>
      <div>two</div>
</div>

I tried removing it by assigning individual id names on them but its impossible to track the changes since its constantly changing and shuffling. 我尝试通过在它们上分配单独的id名称来删除它,但是由于它不断变化和改变,它无法跟踪变化。

The last three can be identified with .slice(-3) on the node list (when converted to array): 可以使用节点列表上的.slice(-3)标识最后三个(转换为数组时):

 const parent = document.querySelector("#name"); [...parent.children].slice(-3).forEach(parent.removeChild.bind(parent)); 
 <div id="name"> <div>one</div> <div>two</div> <div>three</div> <div>four</div> <div>five</div> </div> 

You can select the last 3 divs with :nth-last-child(-n+3) and delete them in an iteration 您可以使用:nth-last-child(-n+3)选择最后3个div,并在迭代中删除它们

 document.querySelectorAll('#name > div:nth-last-child(-n+3)').forEach(n => { n.parentNode.removeChild(n) }) 
 <div id="name"> <div>one</div> <div>two</div> <div>three</div> <div>four</div> <div>five</div> </div> 

You can get all children nodes of #name with document.querySelectorAll('#name>div') . 您可以使用document.querySelectorAll('#name>div')获取#name所有子节点。 The rest should be easy enough: 其余的应该很容易:

 function deleteLast3() { var childrens = document.querySelectorAll('#name>div'); for (var index = 0; index < childrens.length; index++) { if (index >= childrens.length - 3) childrens[index].remove(); } } 
 <div id="name"> <div>one</div> <div>two</div> <div>three</div> <div>four</div> <div>five</div> </div> <button onclick="deleteLast3()">Delete 3</button> 

Loop through the set in reverse and remove the first 3 you find: 反向循环设置并删除您找到的前3个:

 let divs = document.querySelectorAll("#name > div"); // Get all the child divs // Set up loop to go from highest index to lowest, but not to the first two for(var i = divs.length -1; i > 1; i--){ // Remove node at the current index divs[i].parentNode.removeChild(divs[i]); } 
 <div id="name"> <div>one</div> <div>two</div> <div>three</div> <div>four</div> <div>five</div> </div> 

You can use this code: 您可以使用此代码:

EDIT: using parentDivDOM.children you retrieve all the direct chilldren of your parent div. 编辑:使用parentDivDOM.children您检索父div的所有直接chilldren。 Then, starting from the last one, you check if it's a div checking its tag name: if it is, you delete it. 然后,从最后一个开始,检查它是否是div检查其标签名称:如果是,则删除它。

Notice that you also keep track of how many div you have deleted with the count variable. 请注意,您还可以使用count变量跟踪已删除的div count

 const parentDivDOM = document.getElementById("name"); const childrenDOM = parentDivDOM.children; for (let i=childrenDOM.length-1, count=0; i>=0 && count<3; i--) { if (childrenDOM[i].tagName === "DIV") { childrenDOM[i].parentElement.removeChild(childrenDOM[i]); count++; } } 
 <div id="name"> <div>one</div> <div>two</div> <div>three</div> <div>four</div> <div>five</div> </div> 

Although the question for javascript I would suggest to do this with jQuery. 虽然javascript的问题我建议用jQuery做这个。

 $('#name').find("div").slice(-3).remove(); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <div id="name"> <div>one</div> <div>two</div> <div>three</div> <div>four</div> <div>five</div> </div> 

Hope it helps. 希望能帮助到你。

Here is a try 这是一个尝试

 function deleteLast3() { document.querySelectorAll('#name div') .forEach(function (x, i, self){ if (self.length -3 <= i) x.remove(); }); } 
 <div id="name"> <div>one</div> <div>two</div> <div>three</div> <div>four</div> <div>five</div> </div> <button onclick="deleteLast3()">Delete 3</button> 

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

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