add class via jquery but only when not exists

前端 未结 7 942
刺人心
刺人心 2020-12-15 02:25

I would like to add a class (class=\"bbox\") to a ul-list but only if no class exists. This is what i have so far. How do I check with jquery if a class exists in the ul-tag

相关标签:
7条回答
  • 2020-12-15 03:25

    karim79 you almost got it !

    let me correct you

    $("ul:not([class*='bbox'])").addClass("bbox");
    

    You match the tag with absolute no classes, and if ul had other classes the matching wouldn't be made. You got to search for the exactly class you wish to add. Like .hasClass aproach.

    hope it helps

    0 讨论(0)
提交回复
热议问题