Removing multiple classes (jQuery)

前端 未结 8 1963
醉梦人生
醉梦人生 2020-11-30 16:56

Is there any better way to rewrite this:

$(\'element\').removeClass(\'class1\').removeClass(\'class2\');

I cannot use removeC

8条回答
  •  鱼传尺愫
    2020-11-30 17:24

    Since jQuery 3.3.0, it is possible to pass arrays to .addClass(), .removeClass() and toggleClass(), which makes it easier if there is any logic which determines which classes should be added or removed, as you don't need to mess around with the space-delimited strings.

    $("div").removeClass(["class1", "class2"]); 
    

提交回复
热议问题