Get class list for element with jQuery

后端 未结 17 1597
粉色の甜心
粉色の甜心 2020-11-22 03:20

Is there a way in jQuery to loop through or assign to an array all of the classes that are assigned to an element?

ex.

相关标签:
17条回答
  • 2020-11-22 03:36

    I had a similar issue, for an element of type image. I needed to check whether the element was of a certain class. First I tried with:

    $('<img>').hasClass("nameOfMyClass"); 
    

    but I got a nice "this function is not available for this element".

    Then I inspected my element on the DOM explorer and I saw a very nice attribute that I could use: className. It contained the names of all the classes of my element separated by blank spaces.

    $('img').className // it contains "class1 class2 class3"
    

    Once you get this, just split the string as usual.

    In my case this worked:

    var listOfClassesOfMyElement= $('img').className.split(" ");
    

    I am assuming this would work with other kinds of elements (besides img).

    Hope it helps.

    0 讨论(0)
  • 2020-11-22 03:36

    The question is what Jquery is designed to do.

    $('.dolor_spec').each(function(){ //do stuff
    

    And why has no one given .find() as an answer?

    $('div').find('.dolor_spec').each(function(){
      ..
    });
    

    There is also classList for non-IE browsers:

    if element.classList.contains("dolor_spec") {  //do stuff
    
    0 讨论(0)
  • 2020-11-22 03:37

    javascript provides a classList attribute for a node element in dom. Simply using

      element.classList
    

    will return a object of form

      DOMTokenList {0: "class1", 1: "class2", 2: "class3", length: 3, item: function, contains: function, add: function, remove: function…}
    

    The object has functions like contains, add, remove which you can use

    0 讨论(0)
  • 2020-11-22 03:38

    You should try this one:

    $("selector").prop("classList")
    

    It returns an array of all current classes of the element.

    0 讨论(0)
  • 2020-11-22 03:38

    A bit late, but using the extend() function lets you call "hasClass()" on any element, e.g.:
    var hasClass = $('#divId').hasClass('someClass');

    (function($) {
    $.extend({
        hasClass: new function(className) {
            var classAttr = $J(this).attr('class');
            if (classAttr != null && classAttr != undefined) {
                var classList = classAttr.split(/\s+/);
                for(var ix = 0, len = classList.length;ix < len;ix++) {
                    if (className === classList[ix]) {
                        return true;
                    }
                }
            }
            return false;
        }
    }); })(jQuery);
    
    0 讨论(0)
  • 2020-11-22 03:40

    You can use document.getElementById('divId').className.split(/\s+/); to get you an array of class names.

    Then you can iterate and find the one you want.

    var classList = document.getElementById('divId').className.split(/\s+/);
    for (var i = 0; i < classList.length; i++) {
        if (classList[i] === 'someClass') {
            //do something
        }
    }
    

    jQuery does not really help you here...

    var classList = $('#divId').attr('class').split(/\s+/);
    $.each(classList, function(index, item) {
        if (item === 'someClass') {
            //do something
        }
    });
    
    0 讨论(0)
提交回复
热议问题