Execute JS code after pressing the spacebar

后端 未结 5 784
心在旅途
心在旅途 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:54

    The 2019 version of this would be: (works in all major browsers - Chrome, Firefox, Safari)

    Spec link - https://www.w3.org/TR/uievents/#dom-keyboardevent-code

    code holds a string that identifies the physical key being pressed. The value is not affected by the current keyboard layout or modifier state, so a particular key will always return the same value. The un-initialized value of this attribute MUST be "" (the empty string).

    // event = keyup or keydown
    document.addEventListener('keyup', event => {
      if (event.code === 'Space') {
        console.log('Space pressed')
      }
    })

提交回复
热议问题