如何删除除任何特定ID外的元素 [英] How to remove elements except any specific id

查看:59
本文介绍了如何删除除任何特定ID外的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说有一个包含许多元素的父ID,我想删除除一个元素之外的所有元素.

let say there is a parent id which contains many elements and i want to remove all elements except one.

例如. :

ex. :

<div id = "parent_id">
  <div id = "id_1">
      <div id = "id_11"> </div>
      <div id = "id_11"> </div>
  </div>
  <div id = "id_2"> </div> 

  <div id = "id_n"> </div>   // No need to remove this id_n only
</div>

因为我可以像这样删除document.getElementId('parent_id').innerHTML = '';的innerHTML,但是我不需要删除id_n.有什么方法可以使用javascript或jQuery.

As i can remove innerHTML like this document.getElementId('parent_id').innerHTML = ''; but i need not to remove id_n. is there any way to do that using javascript or jQuery.

推荐答案

$("#parent_id").children(":not(#id_n)").remove();

这篇关于如何删除除任何特定ID外的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆