how to change class name of an element by jquery

前端 未结 3 578
野的像风
野的像风 2021-01-31 07:19
<
相关标签:
3条回答
  • 2021-01-31 07:42
    $('.IsBestAnswer').removeClass('IsBestAnswer').addClass('bestanswer');
    

    Your code has two problems:

    1. The selector .IsBestAnswe does not match what you thought
    2. It's addClass(), not addclass().

    Also, I'm not sure whether you want to replace the class or add it. The above will replace, but remove the .removeClass('IsBestAnswer') part to add only:

    $('.IsBestAnswer').addClass('bestanswer');
    

    You should decide whether to use camelCase or all-lowercase in your CSS classes too (e.g. bestAnswer vs. bestanswer).

    0 讨论(0)
  • 2021-01-31 07:54

    Instead of removeClass and addClass, you can also do it like this:

    $('.IsBestAnswer').toggleClass('IsBestAnswer bestanswer');
    
    0 讨论(0)
  • 2021-01-31 07:58
    $('.IsBestAnswer').addClass('bestanswer').removeClass('IsBestAnswer');
    

    Case in method names is important, so no addclass.

    jQuery addClass()
    jQuery removeClass()

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