1

我有一些看起来像这样的html:

<div class="block">
    <div id="item1"></div>
    <div id="item2"></div>
</div>

<div class="block">
    <div id="item3"></div>
</div>

我正在尝试检查我的 div 是否包含特定元素。我需要遍历每个块,然后获取每个项目。

到目前为止我所拥有的

    $('.block:has(div)').each(function() {
            // do something with the block
    });

这很棒,因为它将返回所有包含项目的块。但我需要一些类似的东西

    $('.block:has(div)').each(function() {
             $('current place in loop' + li).each(function() {
                    // do something with each item element, before the block loop has finished
             });
    });

有任何想法吗?

4

4 回答 4

3

我希望你的意思是:

$('.block').each(function(){
    $(this).find('div').each(function(){
        //do your stuff
    });
});
于 2009-05-26T09:22:24.627 回答
2

我不能 100% 确定这是否能回答您的问题,但以下工作不会:

$('.block:has(div)').each(function() {
  $(this).children('div').each(function() {
    // do something with each item element, before the block loop has finished
  });
});
于 2009-05-26T09:23:41.787 回答
2

我会在嵌套的 each 中构建选择器,或者使用 find:

 $('.block:has(div)').each(function() {
     $('#' + this.id +  ' li').each(function() {
         //do stuff on list item            
     });
 });

或者

 $('.block:has(div)').each(function() {
     $(this).find('li').each(function() {
         //do stuff on list item            
     });
 });
于 2009-05-26T09:29:48.247 回答
1

你试过用

$("#" + this)

?
它主要用于 jquery 文档的每个示例(harshath 已经发布......太慢了,呜咽

于 2009-05-26T09:24:38.430 回答