Execute JS code after pressing the spacebar

后端 未结 5 824
心在旅途
心在旅途 2020-12-11 00:21

this is my code in JavaScript:

var changeIdValue =  function(id, value) {
document.getElementById(id).style.height = value;
};

document.getElementById (\"ba         


        
5条回答
  •  我在风中等你
    2020-12-11 00:38

    keyCode is deprecated. You could use key (the character generated by pressing the key) or code (physical key on the keyboard) instead. Using one or another can have different outputs depending on the keyboard layout.

    document.addEventListener('keydown', (e) => {
        if (e.key === "Space") {
            // Do your thing
        }
    });
    

提交回复
热议问题