JavaScript设置控件焦点 [英] JavaScript set control focus

查看:74
本文介绍了JavaScript设置控件焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码,如果按下返回键,我可以将焦点设置在控件上,但不能按下TAB。如何按TAB来设置控制焦点?谢谢。



  if (event.keyCode ==  13  || event.keyCode ==  9  || k ==  0 ){ //   RETURN或TAB  
...
document .getElementById(e.id).focus();

解决方案

正如RyanDev建议的那样,你缺少的是 e.preventDefault()



但是为什么要更改标签上的浏览器行为?



有可访问性问题的人会发现您的网站很难使用。很多有行动不便或视力问题的人都依赖于读者以及标签前进和后退标签的有限导航选择。



相反,你应该管理控件的顺序使用 tabIndex 属性。



tabIndex属性 [ ^

Using the piece of code below, I can set focus on a control if pressing the 'RETURN' key, but not on pressing the 'TAB'. How can setting control focus be done also for pressing 'TAB'? Thanks.

if (event.keyCode == 13 || event.keyCode == 9 || k == 0) {  // RETURN or TAB
   ...
   document.getElementById(e.id).focus();

解决方案

As RyanDev suggested, the bit you're missing is e.preventDefault().

But why would you change the browser behaviour on tab press?

People with accessibility issues are going to find your website very difficult to use. Lots of people with mobility issues or sight issues are dependant on readers and the limited navigation choices of tab forwards and tab backwards.

Instead you should be managing the order of controls using the tabIndex attribute.

tabIndex Attribute[^]


这篇关于JavaScript设置控件焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆