【发布时间】:2016-09-28 03:36:54
【问题描述】:
我有一些具有相同类的 div 元素。我想遍历它们。我正在使用 jquery ".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 不是函数”。如果我不指定索引,我会调整数组的所有元素...我 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">
为什么它不起作用,我应该怎么做才能使它起作用?
【问题讨论】:
标签: javascript jquery html arrays