Check if any ancestor has a class using jQuery

后端 未结 3 723
醉话见心
醉话见心 2020-12-07 11:06

Is there any way in jQuery to check if any parent, grand-parent, great-grand-parent has a class.

I have a markup structure that has left me doing this sort

3条回答
  •  生来不讨喜
    2020-12-07 11:30

    You can use parents method with specified .class selector and check if any of them matches it:

    if ($elem.parents('.left').length != 0) {
        //someone has this class
    }
    

提交回复
热议问题