jQuery: Temporarily change a style then reset to original class

后端 未结 4 1368
一向
一向 2020-12-13 05:33

Say I have a class named testThing:

.testThing
{
   background-color:#000000;
   float:left;
   height:50px;
   width:50px;
}

And I want to

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 06:10

    Jquery adds CSS in the style attribute which has higher priority over what is in your CSS file. You're not changing your CSS document with that function and thus adding and removing and adding the class have no effect since it hasn't been modified at all!

    You should use the same function but set the background color to black this time.

    function reset(this)
    {
      $(this).css('background-color', '#000000');
    }
    

    Or simply remove the style attribute

    function reset(this)
    {
      $(this).removeAttr('style');
    }
    

提交回复
热议问题