jquery - how to check if element has any classes attached to it

后端 未结 8 1240
情深已故
情深已故 2021-01-27 02:20

you would imagine this would be easy to implement, but oh well..

anyway, I have an element to which classes are added or removed based of interaction on the site.

<
8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-27 02:46

    Use this one...

    var classes = $('#mydiv').attr('class').split(/\s+/);
    

    This will give you an array of all the classes applied to your element. For the length you can simply extend it as

    var classes = $('#mydiv').attr('class').split(/\s+/).length;
    

    Cheers

提交回复
热议问题