开发者

jQuery child selector?

开发者 https://www.devze.com 2023-01-18 06:02 出处:网络
I have a collection of div\'s which I need to remember and loop through, easy. var myCollection = $(\'div.myClass\');

I have a collection of div's which I need to remember and loop through, easy.

var myCollection = $('div.myClass');
$.each(myCollection, function(myDiv){...});

Now I want to select some span tags in each of those div's but only those that are direct children of the div. This kinda works...

$.each(myCollection, function(myDiv){
    $('span.error', $(myDiv)).each(function(){...});
});

I don't want it to work in the following scenario

<div class="myClass">
    <div class="myClass">
        <span class="error"></span>
    </div>
</div>

[If I didn't need to save the collection I could have used a child selector div.myClass > spa开发者_如何学Gon.error]


Try this:

$('div.myClass').each(function() {
  $(this).children('span.error').each(function() {
    var span_error = $(this);
  });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消