how to highlight a div for a few seconds using jQuery

前端 未结 5 2208
無奈伤痛
無奈伤痛 2021-02-08 01:04

I want to be add the following to a a page:

When a div is clicked, I want to:

  1. change the background color of the clicked on div for a few seconds
5条回答
  •  执念已碎
    2021-02-08 01:50

    You could use the setTimeout function:

    $('div.highlightable').click(function(){
        var $this = $(this);
        //change background color via CSS class
        $this.addClass('highlighted');
        // set a timeout that will revert back class after 5 seconds:
        window.setTimeout(function() {
            $this.removeClass('highlighted');
        }, 5 * 1000);
    });
    

提交回复
热议问题