Get class list for element with jQuery

后端 未结 17 1633
粉色の甜心
粉色の甜心 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:27

    On supporting browsers, you can use DOM elements' classList property.

    $(element)[0].classList
    

    It is an array-like object listing all of the classes the element has.

    If you need to support old browser versions that don't support the classList property, the linked MDN page also includes a shim for it - although even the shim won't work on Internet Explorer versions below IE 8.

提交回复
热议问题