最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove all the elements outside of a container? - Stack Overflow

programmeradmin0浏览0评论

I'm trying to find a way to remove all the elements (div's) outside of a specific container.

For example:

I have an HTML container with a few div's inside of it like so:

<div id="container">
    <div class="baby"></div>
    <div class="baby"></div>
    <div class="baby"></div>
    <div class="baby"></div>
</div>
<div id="someID">
    <div class="baby"></div>
    <div class="baby"></div>
    <div class="baby"></div>
</div>
<div class="baby"></div>
<div class="baby"></div>
<div class="baby"></div>

I basically need to remove all the elements with the class name baby that is outside of container. Some of the elements don't even have a container so I cannot target them using the parent or anything like that.

Is this possible at all?

I'm trying to find a way to remove all the elements (div's) outside of a specific container.

For example:

I have an HTML container with a few div's inside of it like so:

<div id="container">
    <div class="baby"></div>
    <div class="baby"></div>
    <div class="baby"></div>
    <div class="baby"></div>
</div>
<div id="someID">
    <div class="baby"></div>
    <div class="baby"></div>
    <div class="baby"></div>
</div>
<div class="baby"></div>
<div class="baby"></div>
<div class="baby"></div>

I basically need to remove all the elements with the class name baby that is outside of container. Some of the elements don't even have a container so I cannot target them using the parent or anything like that.

Is this possible at all?

Share edited Apr 23, 2016 at 6:17 Pranav C Balan 115k25 gold badges171 silver badges195 bronze badges asked Apr 23, 2016 at 6:11 JacksonJackson 82017 silver badges38 bronze badges 1
  • 1 We (all humans) are outside of that container. Take care of us! Don't remove us! – mehrandvd Commented Apr 23, 2016 at 7:21
Add a ment  | 

3 Answers 3

Reset to default 10

You can use :not() or not() to avoid the element inside #container

$('.baby:not(#container .baby)').remove();
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="container">
  <div class="baby">1</div>
  <div class="baby">1</div>
  <div class="baby">1</div>
  <div class="baby">1</div>
</div>
<div id="someID">
  <div class="baby">2</div>
  <div class="baby">2</div>
  <div class="baby">2</div>
</div>
<div class="baby">3</div>
<div class="baby">3</div>
<div class="baby">3</div>


Using not()

$('.baby').not('#container .baby').remove();
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="container">
  <div class="baby">1</div>
  <div class="baby">1</div>
  <div class="baby">1</div>
  <div class="baby">1</div>
</div>
<div id="someID">
  <div class="baby">2</div>
  <div class="baby">2</div>
  <div class="baby">2</div>
</div>
<div class="baby">3</div>
<div class="baby">3</div>
<div class="baby">3</div>

//$('.baby:not(#container .baby)').remove();//select class baby not inside container using selector :not

$('.baby').not('#container .baby').remove();//select class baby not inside container using method .not()
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div class="baby">1</div>
<div class="baby">2</div>
<div class="baby">3</div>
<div class="baby">4</div>
</div>

<div id="someID">
<div class="baby">5</div>
<div class="baby">6</div>
<div class="baby">7</div>
</div>


<div class="baby">8</div>
<div class="baby">9</div>
<div class="baby">0</div>

You can also use the container where all the children are located such as:

$('body').children().not('#container').remove();
发布评论

评论列表(0)

  1. 暂无评论