表单元素(input、select 或者 textarea 等)html
以及 document.bodygit
document.activeElement
:返回当前页面中得到焦点的元素,也就是说,若是此时用户按下了键盘上某个键,会在该元素上触发键盘事件。该属性是只读的。github
Chrome 页面加载后默认的焦点元素是 document.bodyruby
https://developer.mozilla.org...code
1)页面交互:点击 input 输入框等。htm
2)JS代码:HTMLElement.focus() 方法能够设置指定元素获取焦点。blog
3)使用 HTML5 定义的新属性 autofocus,可让元素自动得到焦点。事件
例如 <input type="text" autofocus />get
这样 input 会在页面载入后会自动得到焦点。input
注意:普通 DOM 元素,想要得到焦点,须要先设置 tabindex="-1"
属性,再经过页面交互或者 focus 方式,让其得到焦点。
1)页面交互:点击页面其余地方
2)JS代码:HTMLElement.blur() blur方法用来移除当前元素所得到的键盘焦点。
tabindex 属性的做用是:当用 tab 键遍历切换页面的表单元素时,按照 tabindex 的大小决定顺序。
当普通 DOM 元素设置属性 tabindex="-1" 时,可将其成为焦点元素。
更多 tableindex 相关:http://www.cnblogs.com/rubylo...