Jquery change background color

前端 未结 3 1194
耶瑟儿~
耶瑟儿~ 2020-11-29 19:17

I was trying out jquery with this example:

 $(document).ready(function(){
      $(\"button\").mouseover(function(){
        $(\"p#44.test\").css(\"backgroun         


        
3条回答
  •  长情又很酷
    2020-11-29 20:11

    This is how it should be:

    Code:

    $(function(){
      $("button").mouseover(function(){
        var $p = $("#P44");
        $p.stop()
          .css("background-color","yellow")
          .hide(1500, function() {
              $p.css("background-color","red")
                .show(1500);
          });
      });
    });
    

    Demo: http://jsfiddle.net/p7w9W/2/

    Explanation:

    You have to wait for the callback on the animating functions before you switch background color. You should also not use only numeric ID:s, and if you have an ID of your

    there you shouldn't include a class in your selector.

    I also enhanced your code (caching of the jQuery object, chaining, etc.)

    Update: As suggested by VKolev the color is now changing when the item is hidden.

提交回复
热议问题