简体   繁体   English

如何选择这些子div

[英]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.

相关问题 如何选择父元素以及所有非第一个子div以及父母及其子女的兄弟姐妹 - How can I select a parent element, and all child divs that are not the first, as well as siblings of the parents and their children 如何选择以1递增的多个类及其匹配的子类? - How do I select multiple classes incremented by 1 and their matching children? 如何从选择表单中搜索文本的 div 并将 div 的显示更改为无? - How do I search divs for text from a select form and change diplay of the divs to none? 如何使用Javascript获取所有孩子(即孩子的孩子)? - How do i get all children (ie children of children) with Javascript? 循环浏览父级DIV并选择某些子级 - Loop through parent DIVs and select certain children 如何使用 querySelectorAll 对所有 DIV 进行 select? - How can I select all DIVs with querySelectorAll? 如何使用jQuery选择列表中所有具有子元素的元素? - How do I select all of the elements in a list that have children, using jQuery? 如何选择具有相同类(SaSS)的孙子DOM - How do I select the the grand-children DOMs with the same class (SaSS) Javascript:querySelector 只选择 1 个 div。 如何将其发送到 select 所有具有相同 class 的 div? - Javascript: querySelector only selects 1 div. How do I get it to select all divs with the same class? 选择框选项制成后,如何停止JS查询隐藏页面上的所有div? - How do I stop JS query hiding all divs on page when select box option is made?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM