简体   繁体   中英

Add a div only inside the first three divs with jquery

I want to add a new div inside the first three divs with jquery. Is there a way to do this like :nth-child in css?

My example html:

<div class="wrapper">
  <div class="box"><!-- add div --></div>
  <div class="box"><!-- add div --></div>
  <div class="box"><!-- add div --></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>

I know something like:

<script>
$( ".wrapper .box" ).append( "<div>Hello</div>" );
</script>

... but this will add the new div inside all divs ;(.

You can use the :lt() selector

 $(".wrapper .box:lt(3)").append("<div>Hello</div>"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> 

Use slice() method to get first 3 elements collection.

<script>
$( ".wrapper .box" ).slice(0, 3).append( "<div>Hello</div>" );
</script>

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="box"> <!-- add div --> </div> <div class="box"> <!-- add div --> </div> <div class="box"> <!-- add div --> </div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> <script> $( ".wrapper .box" ).slice(0, 3).append( "<div>Hello</div>" ); </script> 


Or with :nth-child pseudo-class selector if divs are siblings.

 <script> $( ".wrapper .box:nth-child(-n+3)" ).append( "<div>Hello</div>" ); </script> 

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="box"> <!-- add div --> </div> <div class="box"> <!-- add div --> </div> <div class="box"> <!-- add div --> </div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> <script> $(".wrapper .box:nth-child(-n+3)").append("<div>Hello</div>"); </script> 

If you need a non-jquery version

var wrapper = document.querySelector('.wrapper');

wrapper.children[0].innerHTML = '<div>Hello</div>';
wrapper.children[1].innerHTML = '<div>Hello</div>';
wrapper.children[2].innerHTML = '<div>Hello</div>';

or another version of the above would be

[].slice.call(document.querySelectorAll('.wrapper .box'),0,3).forEach(function(div){
   div.innerHTML = '<div>Hello</div>'
});

This will get the first 3 divs with box as a class.

for(var i=1;i<4;i++){
$( ".wrapper .box:nth-of-type(" + i + ")" ).append( "<div>Hello</div>" );
}

Edit: APAD1 gave you a better way to do this.

var allBoxes = document.querySelectorAll('.box'); for(var i = 0; i < 3; i++) { allBoxes[i].innerHTML = '<div></div>'; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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