使用jquery获取类名

使用jquery获取类名,jquery,Jquery,在名为$target的变量内的div元素集中,我有单个类的元素。 我想通过抛出每个元素并得到它的类名。 大概是这样的: $.each($target.children(), function(){ //get class from 'this' - how? }); 最好的方法是什么 我不想使用经典的JavaScript(.className) 使用attr函数。 e、 g 这里有一种方法可以处理每个元素的多个类,返回一个没有重复的类列表,并且应该跨浏览器工作(使用$.inArray而

在名为$target的变量内的div元素集中,我有单个类的元素。 我想通过抛出每个元素并得到它的类名。 大概是这样的:

$.each($target.children(), function(){
    //get class from 'this' - how?
});
最好的方法是什么

我不想使用经典的JavaScript(.className)

使用attr函数。 e、 g


这里有一种方法可以处理每个元素的多个类,返回一个没有重复的类列表,并且应该跨浏览器工作(使用
$.inArray
而不是
indexOf

然后:


可能的重复不是重复。不同的问题。它是返回类名数组还是返回带有类名的单个字符串?它将重新生成带有类名的单个字符串。
$.each($target.children(), function(){
    var cls = $(this).attr("class");
});
$.each($target.children(), function(){
    alert($(this).attr("class"))
});
function getUniqueClasses(jqobj) {
  var result = [];
  jqobj.each(function(idx, elem) {
    $.each(elem.className.split(' '), function(i, e) {
      if ($.inArray(e, result) == -1) result.push(e);
    });
  });
  return result.join(','); // if you want a list instead of an array
}
var answer = getUniqueClasses( $('#target').children() );