Jquery 获取所有没有';X';班

Jquery 获取所有没有';X';班,jquery,html,html-lists,Jquery,Html,Html Lists,将html列表作为 <ul id="test"> <li class="A"></li> <li class="B"></li> <li class="C"></li> <li class="empty"></li> <li class="E"></li> <li class="empty"></li

将html列表作为

<ul id="test">
    <li class="A"></li>
    <li class="B"></li>
    <li class="C"></li>
    <li class="empty"></li>
    <li class="E"></li>
    <li class="empty"></li>
    <li class="F"></li>
<ul>
要获取除空类名以外的所有类名。 如何使用jquery获得它? 预期结果,如
A B C E F

您可以使用
.map()
.get()
在数组中收集这些类名,并根据需要显示它们

试试看

尝试:

如果只想针对
li
元素,则:

$('#test').find('li:not(".empty")')
你能试试这个吗

$("#test li:not(.empty)")
代码:

试试这个

$(function()
  {
      $("li:not(.empty)").each(function(){
          alert($(this).attr("Class"));
      });
  });
试试看

var classes = {};
$('#test li:not(.empty)').each(function(){
    //this work only if the target element has one class, if there are multiple classes in an element this has to be redesigned
    classes[this.className] = true;
})
var array = $.map(classes, function(value, key){
    return key;
});

console.log(array)

演示:

我很好奇地问,为什么在这个上下文中要迭代两次。谢谢。只是为了确保类值是唯一的。。。如果有两个元素具有相同的类,他需要类名,而不仅仅是元素。
$("#test li:not(.empty)")
$("#test li:not(.empty)").each(function( index ) {
    console.log( index + ": " + $( this ).attr('class') );
});
$(function()
  {
      $("li:not(.empty)").each(function(){
          alert($(this).attr("Class"));
      });
  });
var classes = {};
$('#test li:not(.empty)').each(function(){
    //this work only if the target element has one class, if there are multiple classes in an element this has to be redesigned
    classes[this.className] = true;
})
var array = $.map(classes, function(value, key){
    return key;
});

console.log(array)
$("#test li:not(.empty)").each(function()
{
    alert($(this).attr("class"));
});