Execute JS code after pressing the spacebar

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

    document.activeElement is whatever element has focus. You'll often find both spacebar and enter firing click on the focused element.

    document.body.onkeyup = function(e){
        if(e.keyCode == 32 || e.keyCode == 13){
            //spacebar or enter clicks focused element
            try {
                doc.activeElement.click();
            }
            catch (e) {
                console.log(e);
            }            
        }
    };  
    

    Then the CSS might be:

    .focusable-thing:hover {
        cursor: pointer;
    }
    .focusable-thing:focus {
        -webkit-box-shadow: 0px 2px 8px 2px rgba(0,0,0,0.4);
        -moz-box-shadow: 0px 2px 8px 2px rgba(0,0,0,0.4);
        box-shadow: 0px 2px 8px 2px rgba(0,0,0,0.4);
    }
    

提交回复
热议问题