简体   繁体   English

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

[英]jQuery using .toggle() to toggle a single element

So I want to toggle a single element in an array of elements how can I do this? 所以我想在一个元素数组中切换单个元素我该怎么做?

What I have tried 我试过了什么

$(".classname")[1].toggle()

The problem with that is that you are getting the element rather than a jquery object. 问题在于你得到的是元素而不是jquery对象。 Try this: 尝试这个:

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

Also, I assume you are looking for the 2nd element using index 1 另外,我假设你正在寻找使用索引1的第二个元素

Try this : 尝试这个 :

$('.classname').eq(0).toggle()

When using [] , you are losing jquery reference and the object become a dom element. 使用[] ,您将丢失jquery引用,并且该对象将成为dom元素。 DOM element are used with Javascript. DOM元素与Javascript一起使用。 Example : 示例:

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

Here the jQuery .eq() information page. 这里是jQuery .eq()信息页面。

Elements are based on a 0 index, wich mean 0 = first element, 1 = second element and go on 元素基于0索引,其中0 = first element, 1 = second element and go on

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM