简体   繁体   中英

how to remove starting div elements using jquery?

<div ="fullcontainer">
<div id="a"></div>
<div id="b"></div>
<div id="c"></div>
<div id="d"></div>
<div id="e"></div>

</div>

$('#fullContainer').children().slice(2).remove();

it gives result :

<div ="fullcontainer">
<div id="a"></div>
<div id="b"></div>
</div>

how to get result like this?

<div ="fullcontainer">

<div id="d"></div>
<div id="e"></div>

</div>

Second Question I got last and second last div id like this :

$('#fullContainer div:last-child').attr('id');

$('#fullContainer div:nth-last-child(2)').attr('id');

How to get first and second div id ?

You can use following code to remove first div:

$(document).ready(function(){
    $("#fullcontainer div:first-child").remove();
});

Checkout Fiddle:

http://jsfiddle.net/aX83W/

Try using .eq() ,

$('#fullContainer').children().eq(0)  //first div
$('#fullContainer').children().eq(1)  //second div
.
.
$('#fullContainer').children().eq(n)  //nth div

Full code for your requirement,

var cache = $('#fullContainer').children();
cache.first().remove();  //removing the first one
cache.eq(cache.length - 2).attr('id'); // getting the id of the second one from the last.

For your new edit you can try like,

   var cache = $('#fullContainer').children();
   cache.filter(':lt(' + (cache.length - 2) + ')').remove();

DEMO

To remove first

$('#fullContainer').children().first().remove();

IDs

$('#fullContainer div:first-child').attr('id');

$('#fullContainer div:nth-child(2)').attr('id');

Use start and end slice() to remove first 3 elements like

$('#fullContainer').children().slice(0,3).remove();// remove first 3 elements

To get first and second element id try to use eq-selector ,

$('#fullContainer div:eq(0)').attr('id');// first element id
$('#fullContainer div:eq(1)').attr('id');// second element id

Or you can try nth-child selector ,

$('#fullContainer div:nth-child(1)').attr('id');// first element id
$('#fullContainer div:nth-child(2)').attr('id');// second element id

Note make sure your div-id is fullcontainer (you missed id from your div element) and you are using fullContainer in jquery, to work properly both must be same.

Demo

$('#fullContainer div').eq(0).remove();

you can use :first or first() or you can use even eq() operator

$('#fullContainer div :first').remove()

or

$('#fullContainer div').first().remove()

or

$('#fullContainer div').eq(0).remove()
To remove first

$('#fullContainer').children().first().remove();

also

$('#fullContainer').children(div:nth-of-type(1)).remove();
$('#fullContainer').children(div:nth-of-type(1)).remove(); 
$('#fullContainer').children(div:nth-of-type(1)).remove();

and even like this to get

$('#fullContainer div:nth-child(1)').attr('id');
$('#fullContainer div:nth-child(2)').attr('id');
$('#fullContainer div:nth-child(n)').attr('id');

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