这对我来说似乎是个愚蠢的问题,但我无法解决....
为什么这样做:
<div style="width:100px; height:100px; border:1px solid black;"
onmouseover="alert('mouseover...');"
>
</div>
但这不是(将鼠标指针移动到 div 字段并按(任意)键后):
<div style="width:100px; height:100px; border:1px solid red;"
onkeydown="alert('keydown...');"
>
</div>
有什么想法吗?谢谢。
最佳答案
按键事件触发当前拥有焦点的元素,而不是鼠标当前指向的元素。
默认情况下,div 元素不是交互式控件。他们根本不会成为焦点。
您可以使用 tabindex
更改它(之后您单击或切换到该元素以使其获得焦点),但这几乎总是一个标志,表明您应该首先使用不同的元素(例如文本区域或按钮)。
The tabindex global attribute is an integer indicating if the element can take input focus (is focusable), if it should participate to sequential keyboard navigation, and if so, at what position. It can takes several values
关于javascript - 为什么 onmouseover 有效,但 onkeydown 无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32336325/