DOM 元素中的焦点管理

栏目: JavaScript · 发布时间: 7年前

内容简介:表单元素(以及 document.body

DOM 元素中的焦点管理

1. 焦点元素

表单元素( inputselect 或者 textarea 等)

以及 document.body

2. 如何查看当前焦点元素

document.activeElement :返回当前页面中获得焦点的元素,也就是说,如果此时用户按下了键盘上某个键,会在该元素上触发键盘事件。该属性是只读的。

Chrome 页面加载后默认的焦点元素是 document.body

https://developer.mozilla.org...

3. 如何让元素获得焦点

1)页面交互:点击 input 输入框等。

2)JS代码: HTMLElement.focus() 方法可以设置指定元素获取焦点。

3)使用 HTML5 定义的新属性 autofocus,可以让元素自动获得焦点。

例如 <input type="text" autofocus />

这样 input 会在页面载入后会自动获得焦点。

注意:普通 DOM 元素,想要获得焦点,需要先设置 tabindex="-1" 属性,再通过页面交互或者 focus 方式,让其获得焦点。

4. 如何让元素失去焦点

1)页面交互:点击页面其他地方

2)JS代码: HTMLElement.blur() blur方法用来移除当前元素所获得的键盘焦点。

5. tabindex 属性

tabindex 属性的作用是:当用 tab 键遍历切换页面的表单元素时,按照 tabindex 的大小决定顺序。

当普通 DOM 元素设置属性 tabindex="-1" 时,可将其成为焦点元素。

更多 tableindex 相关: http://www.cnblogs.com/rubylo...

更多博客: https://github.com/Lmagic16/blog


以上所述就是小编给大家介绍的《DOM 元素中的焦点管理》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Computational Geometry

Computational Geometry

Mark de Berg、Otfried Cheong、Marc van Kreveld、Mark Overmars / Springer / 2008-4-16 / USD 49.95

This well-accepted introduction to computational geometry is a textbook for high-level undergraduate and low-level graduate courses. The focus is on algorithms and hence the book is well suited for st......一起来看看 《Computational Geometry》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具