Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将css类添加到列表中的对象_Javascript_Html - Fatal编程技术网

Javascript 将css类添加到列表中的对象

Javascript 将css类添加到列表中的对象,javascript,html,Javascript,Html,我想知道如何将类添加到列表中的特定对象 让我用一些代码来演示这个问题 $('.box')[5].addClass('center') .center{ 背景色:红色; } c-1 c-2 c-3 c-4 c-5 c-6 c-7 c-8 c-9 $('.box')是一个对象而不是数组。所以你不能像那样使用索引 您只需使用选择器选择匹配集中指定索引处的元素: $('.box:eq(5)').addClass('center') .center{ 背景色:红色; } c-1 c-2 c-3 c

我想知道如何将类添加到列表中的特定对象

让我用一些代码来演示这个问题

$('.box')[5].addClass('center')
.center{
背景色:红色;
}

c-1
c-2
c-3
c-4
c-5
c-6
c-7
c-8
c-9
$('.box')
是一个对象而不是数组。所以你不能像那样使用
索引

您只需使用选择器选择匹配集中指定索引处的元素:

$('.box:eq(5)').addClass('center')
.center{
背景色:红色;
}

c-1
c-2
c-3
c-4
c-5
c-6
c-7
c-8
c-9
$('.box')
是一个对象而不是数组。所以你不能像那样使用
索引

您只需使用选择器选择匹配集中指定索引处的元素:

$('.box:eq(5)').addClass('center')
.center{
背景色:红色;
}

c-1
c-2
c-3
c-4
c-5
c-6
c-7
c-8
c-9

在JQuery中有一种称为

选择匹配集中索引n处的元素

下面是如何在实例中使用它(记住计数从0开始)

$('.box:eq(5)').addClass('center')
.center{
背景色:红色;
}

c-1
c-2
c-3
c-4
c-5
c-6
c-7
c-8
c-9

在JQuery中有一种称为

选择匹配集中索引n处的元素

下面是如何在实例中使用它(记住计数从0开始)

$('.box:eq(5)').addClass('center')
.center{
背景色:红色;
}

c-1
c-2
c-3
c-4
c-5
c-6
c-7
c-8
c-9

为什么每个
循环都是循环?为什么不直接按索引选择单个元素?为什么
每个
循环?为什么不直接按索引选择单个元素?要以选定元素的DOM为目标,您必须wirie
$($('.box')[5])。addClass('center')
要以选定元素的DOM为目标,您必须wirie
$($('.box')[5])。addClass('center')