Change color of button during keypress

后端 未结 3 782
终归单人心
终归单人心 2021-01-24 21:49

I want to change the color of a


when it is clicked by a mouse or while pressing the enter key.<

3条回答
  •  日久生厌
    2021-01-24 22:33

    Use a combination of keypress/keyup to toggle the color:

    $("button").keydown(function(e) {
        // Sets the color when the key is down...
        if(e.which === 13) {
        	$(this).css("background-color", "red");
        }
    });
    $("button").keyup(function() {
        // Removes the color when any key is lifted...
        $(this).css("background-color", "");
    });
    
    

提交回复
热议问题