Javascript 使用.each遍历具有相同类的html元素
我有一些具有相同类的div元素。我想对它们进行迭代。我使用jquery“.each”来实现它。我还想单独访问每个元素,并对其类进行调整,因此我需要在class elements数组中获取元素的索引。我目前有一个类似的代码:Javascript 使用.each遍历具有相同类的html元素,javascript,jquery,html,arrays,Javascript,Jquery,Html,Arrays,我有一些具有相同类的div元素。我想对它们进行迭代。我使用jquery“.each”来实现它。我还想单独访问每个元素,并对其类进行调整,因此我需要在class elements数组中获取元素的索引。我目前有一个类似的代码: $('.the_div_class').each(function(i, obj) { if("a certain condition") { $('.the_div_class')[0].toggleClass('other_div_class'); /
$('.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;
}
}
[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]]
但是,我收到一个错误,提示“$(…)[0]。toggleClass不是函数”。如果我没有指定索引,我将搜索数组的所有元素。。。I console.log“$('.the_div_class')”数组并获得类似于以下内容的结构:
$('.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;
}
}
[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">
为什么它不起作用,我应该怎么做才能使它起作用?代码$('.The_div_class')[0]
将只获取第一个元素,该元素与DOM中的选择器和该类相匹配,它不起作用,因为它不再是jQuery对象(因此它没有方法.toggleClass()
)。在.each()
内部,您可以使用this
引用正在迭代的当前元素:
$('.the_div_class').each(function(i, obj) {
if("a certain condition") {
$(this).toggleClass('other_div_class');
}
}
注意:要在jQuery中通过索引获取项,可以使用。例如:
$('.the_div_class').get(0).toggleClass('other_div_class');
代码$('.u div_class')[0]
将只获取与DOM中的选择器与该类匹配的第一个元素,但它不起作用,因为它不再是jQuery对象(因此它没有方法.toggleClass()
)。在.each()
内部,您可以使用this
引用正在迭代的当前元素:
$('.the_div_class').each(function(i, obj) {
if("a certain condition") {
$(this).toggleClass('other_div_class');
}
}
注意:要在jQuery中通过索引获取项,可以使用。例如:
$('.the_div_class').get(0).toggleClass('other_div_class');
当您指定索引时,您获取的是使用jQuery选择的普通javascript元素,而不是jQuery对象。这就是toggleClass()方法不可用的原因
您可以将它像$($(选择器)[i])那样包装在jQuery中,以将其转换回jQuery对象。然而,每个循环提供的参数在这里是您的朋友。也就是说,您可以使用$(obj)访问循环中的当前对象。当您指定索引时,您获取的是使用jQuery选择的普通javascript元素,而不是jQuery对象。这就是toggleClass()方法不可用的原因 您可以将它像$($(选择器)[i])那样包装在jQuery中,以将其转换回jQuery对象。然而,每个循环提供的参数在这里是您的朋友。也就是说,您可以使用$(obj)访问循环中的当前对象。将代码更改为:
var collection = $('.the_div_class');
collection.each(function(i, obj) {
if("a certain condition") {
$(collection[0]).toggleClass('other_div_class'); // trying to access the index 0 of the array that contains the elements of that class;
}
}
您需要通过再次将DOM元素传递给$来重新创建jQuery对象,即代码的$($('.'u div_class')[0])。将代码更改为:
var collection = $('.the_div_class');
collection.each(function(i, obj) {
if("a certain condition") {
$(collection[0]).toggleClass('other_div_class'); // trying to access the index 0 of the array that contains the elements of that class;
}
}
您需要通过再次将DOM元素传递给$来重新创建jQuery对象,即代码的$($('.the_div_class')[0])。您需要使用以下关键字更改代码以获取元素:
$('.the_div_class').each(function(i, obj) {
if("a certain condition") {
$(this).toggleClass('other_div_class'); // trying to access the index 0 of the array that contains the elements of that class;
}
}
您需要使用以下关键字更改代码以获取元素:
$('.the_div_class').each(function(i, obj) {
if("a certain condition") {
$(this).toggleClass('other_div_class'); // trying to access the index 0 of the array that contains the elements of that class;
}
}