Javascript jQuery 循环遍历子 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11619545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:33:58  来源:igfitidea点击:

jQuery loop through Child divs

javascriptjqueryloopshtmleach

提问by levi

<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>

I want to loop though div.cat_chHow?

我想循环div.cat_ch如何?

This one fails:

这个失败了:

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

采纳答案by scrappedcola

http://jsfiddle.net/2TRxh/

http://jsfiddle.net/2TRxh/

I think your issue lies with the attempt to get the val off the div after you get the attribute $(this).attr("name").val(). Using .val()on a div doesn't make sense. If you remove that $(this).attr("name")returns the nameproperty off the divs. You can further specify the div's to loop through by using the class selector in your each rather than just div. $(".cat_ch").each(function () {});This has been shown in various other answers to this question.

我认为您的问题在于在获得属性后尝试从 div 中获取 val $(this).attr("name").val()。使用.val()上一个div没有意义。如果您删除$(this).attr("name")name属性,则会从 div中返回该属性。您可以通过在 each 中使用类选择器而不仅仅是 div 来进一步指定要循环的 div。$(".cat_ch").each(function () {});这已经在这个问题的其他各种答案中得到了体现。

回答by Ortiga

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

});

Or

或者

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

});

JQuery's .childrenmethod and css3 child selector >will return only the direct children that match the selector, class .cat_chin the example.

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

If you want to search deeper in the DOM tree, that is, include nested elements, use .findor omit the child selector:

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

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

Or

或者

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

回答by Shyju

$(function(){

    var items=$(".cat_ch")
     $.each(items,function (index,item) {

       alert($(item).attr("name"));
    });

});

Working sample : http://jsfiddle.net/GzKHA/

工作示例:http: //jsfiddle.net/GzKHA/

回答by tigertrussell

If you only want to target the Divs inside, try

如果您只想针对里面的 Divs,请尝试

$('#ChosenCategory div').each( function() {...} );

The other answers require specific classes and/or will also process non-divs within your parent div.

其他答案需要特定的类和/或也将处理您的父 div 中的非 div。

回答by spaceman12

    function loopOver(obj)
    {
        var chl=obj.firstChild;
        while(chl)
        {
            if(chl.nodeType==1)
            {
                var isAttr=chl.getAttribute('name');
                if(isAttr)
                {
                    alert(isAttr);
                }
            }
            chl=chl.nextSibling;
        }
    }

    //This is by far the fastest in terms of execution speed!!!
    var obj=document.getElementById("ChosenCategory");
    loopOver(obj);
    
Make sure to enclose the each `<div>` tag at the end of each!!

回答by Marcus

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

回答by jeff

If you want to loop through div.cat_ch, you should use that for the jQuery selector:

如果要遍历div.cat_ch,则应将其用于 jQuery 选择器:

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

You can also loop through the child elements by using the jQuery children()method:

您还可以使用 jQuerychildren()方法循环遍历子元素:

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

A third way to loop through the desired elements is like so:

循环遍历所需元素的第三种方法如下:

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

Use whichever way you want, there is no best way.

使用您想要的任何方式,没有最好的方式。

回答by BlackSpy

$('.cat_ch').each(function(i, e) {
    alert('FW');
    alert(e.attr('name').val());
});