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

后端 未结 8 1251
情深已故
情深已故 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:51

    Use classList property on the HTML Element:

    document.getElementById("mydiv").classList.length
    document.querySelector("#mydiv").classList.length
    $("#mydiv").prop('classList').length // jQuery method.
    

    The classList gives the array of classes present for the element. So, .length will say if there's any class there.

提交回复
热议问题