我如何使用jQuery迭代div的子元素?
我有一个div,它有几个input元素…我想遍历每个元素。 想法?
使用children()和each() ,您可以select性地将select器传递给children 
 $('#mydiv').children('input').each(function () { alert(this.value); // "this" is the current element in the loop }); 
你也可以使用直接的子select器:
 $('#mydiv > input').each(function () { /* ... */ }); 
也可以遍历特定上下文中的所有元素,而不是马赛克嵌套的深度如何:
 $('input', $('#mydiv')).each(function () { console.log($(this)); //log every element found to console output }); 
传递给jQuery“input”select器的第二个参数$('#mydiv')是上下文。 在这种情况下,each()子句将遍历#mydiv容器中的所有input元素,即使它们不是#mydiv的直接子元素。
如果您需要recursion循环子元素:
 function recursiveEach($element){ $element.children().each(function () { var $currentElement = $(this); //////////// Show element console.info($currentElement); //////////// Show events handlers of current element console.info($currentElement.data('events')); //////////// Loop her children recursiveEach($currentElement); }); } //////////// Parent div recursiveEach($("#div")); 
注:在这个例子中,我显示了一个对象注册的事件处理程序。
 它也可以这样做。 
 $('input', '#div').each(function () { console.log($(this)); //log every element found to console output });