javascript - jQuery 循环遍历子 div

标签 javascript jquery loops html each

<div id="ChosenCategory" class="chosen">
   <div class="cat_ch" name="1">
   <div class="cat_ch" name="2">
   <div class="cat_ch" name="3">
   <div class="cat_ch" name="5">
   <div class="clear"> </div>
</div>

我想遍历 div.cat_ch 如何?

这个失败了:

    $("div").each(function () {
       alert("FW");
       alert($(this).attr("name").val());
    });

最佳答案

$('#ChosenCategory').children('.cat_ch').each(function() {

});

或者

$('#ChosenCategory > .cat_ch').each(function() {

});

JQuery 的 .children方法和 css3 子选择器 >将只返回与选择器匹配的直接子级,类 .cat_ch在示例中。

如果要在 DOM 树中进行更深入的搜索,即包含嵌套元素,请使用 .find或省略子选择器:

$('#ChosenCategory').find('.cat_ch').each( function(){} )

或者

$('#ChosenCategory .cat_ch').each( function(){} )

关于javascript - jQuery 循环遍历子 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11619545/

相关文章:

javascript - 解析/过滤带有大量选择选项的 javascript 字符串

jquery - 将输入框文本作为查询字符串传递给 url

jQuery 获取 XML 中的匹配节点

c - 如何提示用户输入一定数量以内的整数

Scalaz 迭代器 : "Lifting" `EnumeratorT` to match `IterateeT` for a "bigger" monad

javascript - "dispatch"未捕获类型错误 React-Redux

javascript - AngularJS 验证 - textarea 上的 ng-minlength

javascript - Kartik Talwar 的 Gmail.js 的 "view_thread"事件不起作用

jquery - 使用 JQuery 将第二个工具栏添加到 JQGrid

loops - 需要帮助从 CASE WHEN 中删除函数