[英]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,所以输出
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. 我已经选择了
main
,我正在尝试使用children
但它不起作用。 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/ http://jsfiddle.net/mfgXG/
Are you forgetting to run after on ready? 你准备好后忘了跑吗?
It does work at least in FF. 它至少在FF中起作用。 Have a look there at this JSFiddle
看看这个JSFiddle
I guess there are more nested div
elements there, ie those "section" elements are not direct children of the main panel? 我想那里有更多嵌套的
div
元素,即那些“section”元素不是主面板的直接子元素?
In such case, have such selector: 在这种情况下,有这样的选择器:
$('#main div[id^="section-"]').each(function(){
To find all matching elements. 查找所有匹配的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.