开发者

JQuery - Browse elements from a main div

开发者 https://www.devze.com 2023-01-15 14:28 出处:网络
i\'ve a list of div linked to a main div-root. How can browse all elements of the main div? example :

i've a list of div linked to a main div-root. How can browse all elements of the main div? example :

<div id="main">
    <div class="trackon" id="trackline1">Line 1</div>
    <div class="trackon" id="trackline2">Line 2</div>
    <div class="trackon" id="trackline3">Line 3</div>
    <div class="trackoff" id="trackline4">Line 4</div>
    <div class="trackoff" id="trackline5">Line 5</div>
</div>

i'm looking for a jquery function that browse all elements (like a for each statament) of #main div. I've tried to search on official documentation but 开发者_StackOverflow中文版i find nothing! cheers


$('#main div').each(function(){
//$(this) is the current div
});

Or, if you want to make sure you just grab first level divs,

$('#main > div').each(function(){
//$(this) is the current div
});


$('#main>div').each(function(){
    alert($(this).attr('id'));
});


$('#main').children().each(function(){
    alert($(this));
});


You can use the each like this:

$('$main > div').each(function(){
  // your code...
});

The > is child selector which is used here to select direct/immediate children of parent div.

0

精彩评论

暂无评论...
验证码 换一张
取 消