Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.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 jQuery使用.toggle()切换单个元素_Javascript_Jquery_Dom_Jquery Selectors_Dom Manipulation - Fatal编程技术网

Javascript jQuery使用.toggle()切换单个元素

Javascript jQuery使用.toggle()切换单个元素,javascript,jquery,dom,jquery-selectors,dom-manipulation,Javascript,Jquery,Dom,Jquery Selectors,Dom Manipulation,所以我想切换元素数组中的单个元素,我该怎么做呢 我试过的 $(".classname")[1].toggle() 试试这个: $('.classname').eq(0).toggle() 使用[]时,您将丢失jquery引用,对象将成为dom元素。DOM元素与Javascript一起使用。例如: $('.classname')[0].id //will work since .id is a DOM attribute $('.classname').eq(0).id //will no

所以我想切换元素数组中的单个元素,我该怎么做呢

我试过的

$(".classname")[1].toggle()
试试这个:

$('.classname').eq(0).toggle()
使用
[]
时,您将丢失jquery引用,对象将成为dom元素。DOM元素与Javascript一起使用。例如:

 $('.classname')[0].id //will work since .id is a DOM attribute
 $('.classname').eq(0).id //will not work since it's a jQuery object
这是这一页


元素基于一个0索引,它的意思是
0=第一个元素,1=第二个元素,然后继续

问题是您得到的是元素而不是jquery对象。试试这个:

$(".classname").eq(1).toggle()

另外,我假设您正在使用索引1查找第二个元素