
<div ID="multi-picker"> <div ID="opt1"> <input type="checkBox"> Option 1 </div> <div ID="opt2"> <input type="checkBox"> Option 2 </div> etc...</div>
一切正常,但我对键盘导航不满意.要从opt1导航到opt2,我需要按Tab键.理想情况下,我想将选项视为选择并使用向上/向下箭头导航.可以这样做吗?
另外…
是否有任何方法可以使用带有复选框样式的选项进行多选,以反映每个选项的选择状态?
function keypressed(e) { var srcElement = e.target; // get the element that fired the onkeydown function var dataset = false; var selectList = false; var next = ""; var prev = ""; if (srcElement.dataset) { // can we use HTML5 dataset? dataset = true; // remember for later // is this an element for which we care if (srcElement.dataset.selectList == 'true') { selectList = true; } } else { // can't use HTML5 dataset,use getAttribute if (srcElement.getAttribute('data-selectList') == 'true') { selectList = true; } } // is it a select element and the user pressed either up arrow or down arrow if (selectList && (e.keyCode == '38' || e.keyCode == '40')) { // get the next and prev navigation options for this element if (dataset) { next = srcElement.dataset.next; prev = srcElement.dataset.prev; } else { next = srcElement.getAttribute('data-next'); prev = srcElement.getAttribute('data-prev'); } // up arrow was pressed and a prev element is defined if (e.keyCode == '38' && prev != '') { document.getElementByID(prev).focus(); } // down arrow was pressed and a next element is defined if (e.keyCode == '40' && next != '') { document.getElementByID(next).focus(); } // don't do native processing of the up or down arrow (page scrolling) e.preventDefault; }}document.onkeydown = keypressed; 这是包含其他元素的新HTML:
<div ID="multi-picker"> <div ID="opt1"> <input ID="select1" type="checkBox" data-selectList="true" data-prev="" data-next="select2"> Option 1 </div> <div ID="opt2"> <input ID="select2" type="checkBox" data-selectList="true" data-prev="select1" data-next=""> Option 2 </div></div>
此代码非常特定于所提出的问题,虽然它可以解决问题,但使用通用插件可能会更好,这样可以在整个环境中实现更广泛的应用.您可能还会遇到与用户期望向下和向上箭头键相关的问题以及通过拦截它们所做的事情相关的问题.
根据我的经验,我遇到了一些问题,其中不同的浏览器甚至不同的最终用户平台对应用程序呈现不同的行为,使得实现一致性不稳定.许多插件旨在消除这种不一致性,并提供更清晰,更直观的界面.
总结以上是内存溢出为你收集整理的html – 如何在div之间实现箭头键导航全部内容,希望文章能够帮你解决html – 如何在div之间实现箭头键导航所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)