有没有办法让所有 onclick 元素都可以通过键盘导航?
对于链接和表单字段,您可以使用 tab
循环槽并激活它们。
然而,在我的代码中,我使用了很多元素,这些元素具有 onclick
属性来执行不同的操作。例如一个table
,其中每个tr
都是可点击的,并在点击时展开。
现在我希望我的页面也只能通过键盘浏览。
我正在考虑在每个 tr
上设置 tabindex
,这在 Firefox 中有效(我可以通过选项卡浏览项目,但不能单击它们),但在 Chrome 中却不行。
如何使用键盘循环浏览所有包含 onclick 的元素?如果不是使用纯 HTML,也许可以使用 JQuery。
With links and form fields you can use tab
to cycle trough and activate them.
In my code however I use lots of elements, that have an onclick
attribute to perform different actions. For example a table
, where each tr
is clickable and expands on click.
Now I want my page to be browseable by keyboard only as well.
I was thinking about setting tabindex
on each tr
, which worked in Firefox (I was able to tab through the items, but not click them), but did not in Chrome.
How can I cycle through all elements containing onclick using the keyboard? If not with plain HTML, maybe with JQuery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我建议您只需将元素包装在锚标记 (
) 中。
I'd recommend that you simply wrap the elements in anchor tags (
<a>
).小提琴。 Tabindex 对我来说适用于 chrome 12。
你的实际问题是什么?
fiddle. Tabindex works in chrome 12 for me.
What's your actual problem?
您可以做的就是选择
onclick
来跟踪当前元素,每次用户点击 - 比方说 ~ 按钮 - 它将移动到下一个元素,当他点击 Enter 时 -只需触发该元素的.click();
即可。我在jsfiddle上设置了一个示例: http://jsfiddle.net/dvirazulay/4kJnM/
这是一种重新实现 tabindex,你可以做得更好,并根据你的喜好更改实际的选择器......
What you can possibly do, is keep track of the current element with
onclick
selected, and everytime the user hits - lets say the ~ button - it will move to the next one, and when he hits Enter - just trigger the.click();
for that element.I've set up an example on jsfiddle: http://jsfiddle.net/dvirazulay/4kJnM/
This is sort of re-implementing tabindex, and you could do it better and change the actual selector to your liking...
像这个小提琴之类的东西?
jQuery
Something like this fiddle?
jQuery
为什么不在该行上放置一个按钮来执行与该行的 onclick 相同的操作?
屏幕阅读器也会拾取按钮(或链接)。
Why not put a button on the row that will do the same action as the onclick of the row?
A button (or a link) will also get picked up by a screen reader.
使用
tabindex="0"
允许您使用 Tab 键导航并将焦点移动到元素/行。但是,无法通过键盘触发onclick
事件。要克服这个问题,请添加一个触发
onclick
事件的onkeydown
事件:您需要过滤掉除
space
(32) 和模仿点击时输入
(13)。这个监听器最好放入脚本中而不是内联,但我只是在这里演示这个概念。
Using
tabindex="0"
allows you to navigate and move focus to the element/row with the tab-key. However, there is no way to trigger theonclick
event by keyboard.To overcome this, add an
onkeydown
event that triggers theonclick
event:You need to filter out other keypresses, except for
space
(32) andenter
(13) when mimicing a click.This listener would be better to put in a script rather than inline, but i'm just demonstrating the concept here.
我希望这样的东西对你有用(这对我不起作用,我在 JS 方面很差)。您可以编辑它,您将得到一个填充表格元素背景的不可见按钮,并告诉用户您可以选择哪个元素有边框。
I hope something like this might work for you, (it does not for me, i am poor at JS). You could edit it and you would get an invisible button filling the background of the table element and to tell the user which is selected you could have borders.
我知道这个问题很旧,但这是我的解决方案。
这样你就可以动态地使用它:
I know this question is old, but this is my solution.
That way you can use it dynamically: