Try checking if target
is the body:
window.addEventListener('keydown', function(e) {
if(e.keyCode == 32 && e.target == document.body) {
e.preventDefault();
}
});
body { height: 100000px; }
<input />
<textarea></textarea>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…