how to add and remove css class

后端 未结 5 1661
清歌不尽
清歌不尽 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:30

    For IE10 and up use these methods:

    // adds class "foo" to el
    el.classList.add("foo");
    
    // removes class "foo" from el
    el.classList.remove("foo");
    
    // toggles the class "foo"
    el.classList.toggle("foo");
    
    0 讨论(0)
  • 2021-01-21 06:38

    Try to add a period before your css class definition.

    Using your example above you should use:

    .messageContainer{
    height:26px;    
    color:#FFFFFF;  
    BACKGROUND-COLOR: #6af; 
    VERTICAL-ALIGN: middle;
    TEXT-ALIGN: center;
    PADDING-TOP:6px;    
    }
    
    0 讨论(0)
  • 2021-01-21 06:42

    You aren't dealing with a class. You have default rules applied to the ID element. As such, you should really only need to add the new class:

    $("#messageContainer").addClass("newRules");
    

    Any rules that aren't overwritten can be overwritten with the css() method:

    $("#messageContainer").css({
      'font-weight':'bold',
      'color':'#990000'
    }).addClass("newRules");
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-21 06:47

    You are not defining a css class in your example, you are using the selector for the id.

    to use a class your code would be like:

    <div id="messageContainer" class="messageContainer"></div>
    

    and in your stylesheet or between style tags you would have

    .messageContainer{  
      height:26px;  
      color:#FFFFFF;    
      BACKGROUND-COLOR: #6af; 
      VERTICAL-ALIGN: middle;
      TEXT-ALIGN: center;
      PADDING-TOP:6px;  
    }
    

    then, using jquery you could remove this class by doing

    $('#messageContainer').removeClass('messageContainer');
    
    0 讨论(0)
提交回复
热议问题