.delay() not working when changing background-color of

前端 未结 3 853
半阙折子戏
半阙折子戏 2021-01-22 06:40

I am just learning JQUERY and I have been playing around with delay() I wrote a fiddle to show you... What I am trying to do is when a button is clicked, change the background-c

相关标签:
3条回答
  • 2021-01-22 06:58

    delay only works for items in the queue (such as animations).

    For anything else, use a regular old timer:

    $("#change").click(function() {
        var $el = $(".animation");
    
        $el.css("background", "blue");
    
        setTimeout(function () {
            $el.css("background", "red");
        }, 700);
    });
    
    0 讨论(0)
  • 2021-01-22 07:05

    You can use delay with queue here

      $(".animation").css("background","blue").delay(700)
             .queue(function() {
                $(this).css("background", "red").dequeue();
       });
    

    Wrap above snippet inside click handler.

    0 讨论(0)
  • 2021-01-22 07:19
    <div id="lock"></div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('#lock').prepend('<tr><td>hello</td><td>cool</td><td>dad</td></tr>');
            $("#lock tr").css('background-color','yellow');
        $("#lock tr")
        .delay(4000)
        .queue(function() {
            $(this).css("background-color","red").dequeue();
        })
        .fadeIn();
    
    });
    
    </script>
    

    Try this.

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