Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让div或span标记在Javascript中接收keydown事件_Javascript_Html - Fatal编程技术网

如何让div或span标记在Javascript中接收keydown事件

如何让div或span标记在Javascript中接收keydown事件,javascript,html,Javascript,Html,比如说, <div onkeydown='alert(3)' style="background:yellow;height:300px;width:300px;"> Click at me first. and press a key to see the event </div> <div onkeydown='alert(3)' tabindex="0" style="background:yellow;height:300px;width:300px;"&

比如说,

<div onkeydown='alert(3)' style="background:yellow;height:300px;width:300px;">
Click at me first. and press a key to see the event
</div>
<div onkeydown='alert(3)' tabindex="0" style="background:yellow;height:300px;width:300px;">
Click at me first. and press a key to see the event
</div>

先点击我。然后按一个键查看事件

这是一个

您可以在上面指定选项卡索引

比如说,

<div onkeydown='alert(3)' style="background:yellow;height:300px;width:300px;">
Click at me first. and press a key to see the event
</div>
<div onkeydown='alert(3)' tabindex="0" style="background:yellow;height:300px;width:300px;">
Click at me first. and press a key to see the event
</div>

先点击我。然后按一个键查看事件

出于某种原因,最简单的JS不能在JSFIDLE上工作。看这里:


函数keyD(){
document.getElementById(“黄色”).style.backgroundColor=“蓝色”;
}
函数keyU(){
document.getElementById(“黄色”).style.backgroundColor=“绿色”;
}