我知道如何让 div 在点击时引用一个位置
<div onclick="location.href='newurl.html';" tabindex="0"><a>Text</a></div>
但仅当通过鼠标单击时它才有效,但如果我想通过键盘导航并按“Enter键”或“空格键”,它将不起作用..
所以我想要的是这样的
<div onkeydown="location.href='newurl.html';" tabindex="0"><a>Islam</a></div>
我不想在其他地方编写代码,比如 js 文件
最佳答案
只需将两个监听器附加到 div
,然后在 onkeydown
中检查按下的键:
<div
onkeydown="if(event.which===32||event.which===13) location.href='newurl.html';"
onclick="location.href='newurl.html';"
tabindex="0"
>
<a>Islam</a>
</div>
关于javascript - 在 DIV 上按下按键以引用某个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20920193/