Jus*_*tin 1 javascript recursion jquery
我是jQuery的新手,熟悉PHP和CSS.我有嵌套的,动态生成的div,我想发送(id)到服务器端脚本来更新数字.我想检查.content类中的所有内容.只有带有id的div才能被发送处理; 但是我在制作递归子()检查时遇到了麻烦...这是我能做的最好的(非递归):
$(".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'));
});
}
});
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
它只是提醒()它们所在级别的所有内容的id.必须有更好的方法来做到这一点...提前感谢您的任何建议.-Justin
尝试:
var ids = $('.content div[id]').map(function() {
return this.id;
}).get();
Run Code Online (Sandbox Code Playgroud)
.content div[id]将选择具有非空ID属性的.content的所有div后代(在任何级别).该[id]部分是Has属性选择器的示例.
我用过.map.提取匹配的ID,.get()并将结果对象转换为基本数组.
在这里试试:http://jsfiddle.net/M96yK/