简体   繁体   中英

How do I select these children divs

I'm trying to alert the ids of each of these divs so it output 11 25 78 .

<div id="main">
  <div id="section-11">Some content</div>
  <div id="section-25">Some content</div>
  <div id="section-78">Some content</div>
</div>

I've already selected main and I'm trying to use children but it's not working. Not sure why.

$('#main').children().each(function(){
   alert($(this).attr('id'));
});
$('#main div').each(function() {
    alert($(this).attr('id').replace(/section-/, ''));
});

This works fine for me:

http://jsfiddle.net/mfgXG/

Are you forgetting to run after on ready?

It does work at least in FF. Have a look there at this JSFiddle

I guess there are more nested div elements there, ie those "section" elements are not direct children of the main panel?

In such case, have such selector:

$('#main div[id^="section-"]').each(function(){

To find all matching elements.

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