我有几个带有 class='CCC' 的 Div。我想使用 jQuery 将所有这些 div 放入一个数组中,然后遍历该数组。怎么做。
最佳答案
// get an array of the divs (will act like one anyway)
var divs = $('div.CCC');
// do something for each div
divs.each(function() {
// this refers to the current div as we loop through
doSomethingWith(this);
});
// or call your method on the array
LoopThroughDivs(divs);
或者,这些可以写为单个语句(如果您只想执行其中之一):
$('div.CCC').each(function() {
// this refers to the current div as we loop through
doSomethingWith(this);
});
LoopThroughDivs($('div.CCC'));
关于jQuery 循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/829112/