是否可以在输入以外的任何内容上添加 keydown 事件
Is it possible to add keydown event on anything other than an input
<div type="text" id="demo">
Hi
</div>
<script>
document.getElementById("demo").addEventListener("keypress", myFunction);
function myFunction() {
document.getElementById("demo").style.backgroundColor = "red";
}
</script>
我正在尝试在按下某个键时更改 div 演示的颜色
您可以通过将属性 tabindex="1" 添加到 div
**更新:**这是一个js fiddle
https://jsfiddle.net/dsm97po9/
<div tabindex="1" id="demo">
试试看告诉我
<div type="text" id="demo">
Hi
</div>
<script>
document.getElementById("demo").addEventListener("keypress", myFunction);
function myFunction() {
document.getElementById("demo").style.backgroundColor = "red";
}
</script>
我正在尝试在按下某个键时更改 div 演示的颜色
您可以通过将属性 tabindex="1" 添加到 div
**更新:**这是一个js fiddle
https://jsfiddle.net/dsm97po9/
<div tabindex="1" id="demo">
试试看告诉我