[英]jQuery Select One Element From Class name
I am in a position where I need to use the .slideToggle()
function in jQuery, on a regular JavaScript determined element. 我处于一个需要在常规JavaScript确定的元素上使用jQuery中的.slideToggle()
函数的位置。 I can use this code: 我可以使用这段代码:
var feedback = document.getElementsByClassName('feedback');
and then a bit later on in a function: 然后稍后在一个函数中:
feedback[index].style.display = 'block';
However, what I want to do is use the slideToggle('fast')
function on feedback[index]
, so instead of so brutally changing its display
to block
, I get a nice jQuery-esque transition. 但是,我想要做的是在feedback[index]
上使用slideToggle('fast')
函数,所以不是如此粗暴地将其display
更改为block
,我得到了一个很好的jQuery-esque过渡。
Obviously this code won't work: 显然这段代码不起作用:
feedback[index].slideToggle('fast');
However this will: 但是这会:
$('.feedback').slideToggle('fast');
but I can't choose which feedback by index to run the slideToggle()
function on, it just does them all, which makes sense. 但我无法通过索引选择哪个反馈来运行slideToggle()
函数,它只是完成所有这些,这是有道理的。 If I could get some code that effectively does this: 如果我能得到一些有效地执行此操作的代码:
$('.feedback')[index].slideToggle('fast');
That would be perfect. 那将是完美的。 I like the fact that I can stick a class on something and iterate through the list of items that appear in .getElementsByClassName('classname')
, so I don't have to stick an ID on everything of the same class, and it would be nice if I could choose which $('.feedback')
element I am using in the list of all elements returned by this but I cannot figure out how that would work. 我喜欢这样一个事实:我可以在某个东西上粘贴一个类并迭代出现在.getElementsByClassName('classname')
中的项目列表,所以我不必在同一个类的所有内容上粘贴一个ID,它会如果我可以选择我在这个返回的所有元素的列表中使用哪个$('.feedback')
元素,那就太好了但是我无法弄清楚它是如何工作的。 If I can somehow choose by index which items in a list by class, to run jQuery commands on it would make this a lot simpler as I do not want to stick an ID on each and every item that has the class of feedback
. 如果我能以某种方式通过索引选择列表中的哪些项目,在其上运行jQuery命令会使这更简单,因为我不想在每个具有feedback
类别的项目上粘贴ID。
Thanks a lot. 非常感谢。
试试这个:你可以使用eq()来选择具有特定索引的元素。
$('.feedback:eq('+index+')').slideToggle('fast');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.