简体   繁体   English

带有 tabindex 的 DIV:捕获关键事件但跳过选项卡

[英]DIV with tabindex: capture key event but skip on tab

I need a <div> that should be able to get key down events.我需要一个<div>应该能够获取按键事件。 For that reason I have to add the attribute tabindex="0" , as suggested here .出于这个原因,我必须添加属性tabindex="0" ,如建议here

The "collateral effect" is that the <div> become "tabbable" and this is not desired because tab should just traverse input fields. “附带效应”是<div>变为“可制表”,这是不希望的,因为制表符应该只遍历输入字段。

Is there a "trick" to avoid it or the only way is to catch the tab event, search for the next input field (so skipping DIVs) and than prevent the default behavior?是否有避免它的“技巧”,或者唯一的方法是捕获选项卡事件,搜索下一个输入字段(因此跳过 DIV)而不是阻止默认行为?

You can test with this.你可以用这个来测试。

 function doSomething(ctrl) { // Prevent default behaviour event.preventDefault(); if (event.key === 'Enter') { ctrl.innerHTML = 'Enter is pressed'; } else if (event.key === 'ArrowDown') { ctrl.innerHTML = 'ArrowDown is pressed'; } }
 <div onkeydown="doSomething(this);" contenteditable="true" readonly style="outline:none;">flsdkjflsajdflkasjdlfkjsaldfkjlksadfj</div>

What about setting tabindex="-1"?设置 tabindex="-1" 怎么样? Seems to work in FF an Chrome似乎在 FF 和 Chrome 中工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM