迭代遍歷相同類的html元素我有一些具有相同類的div元素。我想遍歷它們。我使用jquery「.each」來完成它。我也想要單獨訪問每個元素,並通過它來調用它的類,以便我需要獲取類元素數組中元素的索引。目前,我有一個類似的代碼:使用.each
$('.the_div_class').each(function(i, obj) {
if("a certain condition") {
$('.the_div_class')[0].toggleClass('other_div_class'); // trying to access the index 0 of the array that contains the elements of that class;
}
}
但是我收到一個錯誤說「$(...)[0] .toggleClass不是一個函數」。如果我不指定索引我的toogle陣列中的所有元素......我CONSOLE.LOG的「$(」 the_div_class。)」數組,並得到類似這樣的結構:
[div.the_div_class, div.the_div_class, div.the_div_class, div.the_div_class, div.the_div_class, prevObject: r.fn.init[1]]
而且如果我CONSOLE.LOG「$(」。the_div_class')[0]」我得到這個:
<div class="the_div_class">
爲什麼它不工作,我應該爲了使其發揮作用呢?