2010-12-09 38 views
1

我是jQuery的新手,熟悉PHP & CSS。我嵌套,动态生成的div,我希望发送(id)到服务器端脚本来更新数字。我想检查.content类中的所有内容。只有带ID的div应该被发送处理;但是我无法做一个递归儿童()检查......这是最好的(非递归),我可以这样做:递归的.children()来搜索id属性

$(".content").children().each(function() { 
    if ($(this).attr('id').length == 0) { 
     $(this).children().each(function() { 
      if ($(this).attr('id').length == 0) { 
       $(this).children().each(function() { 
        if ($(this).attr('id').length == 0) { 
         $(this).children().each(function() { 
          alert($(this).attr('id')); 
         }); 
        } 
       }); 
      } 
     }); 
    } 
}); 

,它只是警告()的标识的一切在的他们应该在哪里。必须有更好的方法来做到这一点...提前感谢您的任何建议。 -Justin

回答

5

尝试:

var ids = $('.content div[id]').map(function() { 
    return this.id; 
}).get(); 

.content div[id]会选择你。内容的所有的div后代(任何级别)与非空ID属性。 [id]部分是Has Attribute选择器的示例。我已经使用.map。提取匹配的ID,并将.get()转换为基本数组。

尝试在这里:http://jsfiddle.net/M96yK/

+0

通过警报(IDS)证实jQuery对象;.感谢您的及时回复。 – Justin 2010-12-09 11:49:49

1

你可以这样做:

$(".content div[id]"); 

这将返回一个包含所有div的具有ID的指定