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

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

    You have to check if class attribute:

    • Present but no class( class="")
    • Not present(undefined)

    .

    // with class attribute but no class
    // even without class attribute so no class attr = $("#mydiv").attr("class") hasClass = (attr == undefined || attr.replace(/\s+/, '') == "" )

提交回复
热议问题