jQuery,使用.each获取类中每个元素的ID? [英] jQuery, get ID of each element in a class using .each?
问题描述
我正在尝试获取class
中每个元素的id
,但是相反,它分别警告类的每个名称,因此对于class="test"
,它是警告:t
,e
,s
,t
...关于如何获取属于class
的每个元素id
的任何建议,我们都感激不尽,谢谢.
I'm trying this to get the id
of each element in a class
but instead it's alerting each name of the class separately, so for class="test"
it's alerting: t
, e
, s
, t
... Any advice on how to get the each element id
that is part of the class
is appreciated, as I can't seem to figure this out.. Thanks.
$.each('test', function() {
alert(this)
});
推荐答案
尝试一下,将.myClassName
替换为类的实际名称(但以句点开头).
Try this, replacing .myClassName
with the actual name of the class (but keep the period at the beginning).
$('.myClassName').each(function() {
alert( this.id );
});
因此,如果班级为测试",则可以执行$('.test').each(func...
.
So if the class is "test", you'd do $('.test').each(func...
.
这是.each()
的特定形式,它遍历jQuery对象.
This is the specific form of .each()
that iterates over a jQuery object.
您使用的表单会在任何类型的集合中进行迭代.因此,您实质上是在遍历字符数组t,e,s,t
.
The form you were using iterates over any type of collection. So you were essentially iterating over an array of characters t,e,s,t
.
使用$.each()
的 that 形式,您需要这样做:
Using that form of $.each()
, you would need to do it like this:
$.each($('.myClassName'), function() {
alert( this.id );
});
...与上面的示例具有相同的结果.
...which will have the same result as the example above.
这篇关于jQuery,使用.each获取类中每个元素的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!