how to add and remove css class

后端 未结 5 1674
清歌不尽
清歌不尽 2021-01-21 06:18

how to remove CSS default class

This is my div

This is my css class

#messageContai         


        
5条回答
  •  不知归路
    2021-01-21 06:47

    You can approach it two ways. One, use two classes and literally swap them out for each other:

    .red { background: red }
    .green { background: green }
    

    And then in jQuery:

    $("#messageContainer").attr('class','green'); // switch to green
    $("#messageContainer").attr('class','red'); // switch to red
    

    Or you can use CSS order to toggle a single class:

    #messageContainer { background: red }
    #messageContainer.green { background: green }
    

    Then:

    $("#messageContainer").toggleClass("green");
    

    That would alternate backgrounds every time it was called.

提交回复
热议问题