JavaScript 焦点管理详解:获取、设置与事件处理
本文详细解析 JavaScript 中的焦点管理机制,涵盖如何使非表单元素获取焦点、document.activeElement 属性的用法、focus() 与 blur() 方法的操作,以及 focusin/focusout 等焦点事件的冒泡特性与 tabindex 优先级设置,帮助开发者实现精确的页面交互控制。
焦点元素与获取方式
焦点元素
在默认情况下,仅表单元素能够获取焦点,这是因为只有表单元素具备交互能力。
<input type="text" value="223">
若需让非表单元素获取焦点,可先将 tabIndex 属性设置为 -1,随后调用 focus() 方法。
<div id="test" style="height:30px;width:100px;background:lightgreen">div</div>
<button id="btn">div元素获得焦点</button>
<script>
btn.onclick = function(){
test.tabIndex = -1;
test.focus();
}
test.onfocus = function(){
this.style.background = 'pink';
}
</script>
activeElement 属性
activeElement
document.activeElement 属性用于管理 DOM 焦点,它保存着当前获得焦点的元素引用。
[注意] 该属性在 IE 浏览器中不支持。
<div id="test" style="height:30px;width:100px;background:lightgreen">div</div>
<button id="btn">div元素获得焦点</button>
<script>
console.log(document.activeElement);//<body>
btn.onclick = function(){
console.log(document.activeElement);//<button>
test.tabIndex = -1;
test.focus();
console.log(document.activeElement);//<div>
}
</script>
元素获取焦点的四种方式
获得焦点
元素获取焦点的方式共有 4 种,分别包括页面加载、用户输入(按 Tab 键)、focus() 方法以及 autofocus 属性。
【1】页面加载
默认情况下,当文档刚刚加载完成时,document.activeElement 中保存的是 body 元素的引用。而在文档加载期间,document.activeElement 的值为 null。
<script>
console.log(document.activeElement);//null
</script>
<body>
<script>
console.log(document.activeElement);//<body>
</script>
</body>
【2】用户输入(按 Tab 键)
用户通常可以使用 Tab 键移动焦点,并使用空格键激活焦点。例如,如果焦点位于某个链接上,此时按一下空格键,就会跳转到该链接。
提到 Tab 键,就不能不介绍 tabindex 属性。tabindex 属性用于指定当前 HTML 元素节点是否被 Tab 键遍历,以及遍历的优先级。
1、如果 tabindex=-1,Tab 键将跳过当前元素。
2、如果 tabindex=0,表示 Tab 键将遍历当前元素。如果一个元素没有设置 tabindex,其默认值就是 0。
3、如果 tabindex 大于 0,表示 Tab 键优先遍历。值越大,表示优先级越小(即越先被遍历)。
在下列代码中,使用 Tab 键时,button 获得焦点的顺序是 2、5、1、3。
<div id="box">
<button tabindex= "3">1</button>
<button tabindex= "1">2</button>
<button tabindex= "0">3</button>
<button tabindex= "-1">4</button>
<button tabindex= "2">5</button>
</div>
<script>
box.onkeyup = function(){
document.activeElement.style.background = 'pink';
}
</script>
【3】focus()
focus() 方法用于将浏览器的焦点设置到表单字段,即激活表单字段,使其可以响应键盘事件。
[注意] 前面介绍过,若非表单元素,设置为 tabIndex 为 -1,也可以获取焦点。
<span id="test1" style="height:30px;width:100px;">span</span>
<input id="test2" value="input">
<button id="btn1">span元素获得焦点</button>
<button id="btn2">input元素获得焦点</button>
<script>
btn1.onclick = function(){test1.tabIndex=-1;test1.focus();}
btn2.onclick = function(){test2.focus();}
</script>
【4】autofocus
HTML5 表单字段新增了一个 autofocus 属性,只要设置这个属性,不用 JavaScript 就能自动把焦点移动到相应字段。
[注意] 该属性只能用于表单元素,普通元素即使设置 tabIndex=”-1″ 也不生效。
<input autofocus value="abc">
hasFocus() 方法
hasFocus()
document.hasFocus() 方法返回一个布尔值,表示当前文档之中是否有元素被激活或获得焦点。通过检测文档是否获得了焦点,可以知道是不是正在与页面交互。
console.log(document.hasFocus());//true
//在两秒钟内点击其他标签页,使焦点离开当前页面
setTimeout(function(){
console.log(document.hasFocus());//false
},2000);
失去焦点与 blur() 方法
失去焦点
如果使用 JavaScript 来使元素失去焦点,那么就要使用 blur() 方法。
blur() 方法的作用是从元素中移走焦点。在调用 blur() 方法时,并不会把焦点转移到某个特定的元素上;仅仅是将焦点从调用这个方法的元素上面移走而已。
<input id="test" type="text" value="123">
<button id="btn1">input元素获得焦点</button>
<button id="btn2">input元素失去焦点</button>
<script>
btn1.onclick = function(){test.focus();}
btn2.onclick = function(){test.blur();}
</script>
焦点事件详解
焦点事件
焦点事件会在页面获得或失去焦点时触发。利用这些事件并与 document.hasFocus() 方法及 document.activeElement 属性配合,可以知晓用户在页面上的行踪。
焦点事件共包括下面 4 个:
1、blur
blur 事件在元素失去焦点时触发。这个事件不会冒泡。
2、focus
focus 事件在元素获得焦点时触发。这个事件不会冒泡。
3、focusin
focusin 事件在元素获得焦点时触发。这个事件与 focus 事件等价,但它会冒泡。
4、focusout
focusour 事件在元素失去焦点时触发。这个事件与 blur 事件等价,但它会冒泡。
[注意] 关于 focusin 和 focusout 事件,除了 IE 浏览器支持 DOM0 级事件处理程序,其他浏览器都只支持 DOM2 级事件处理程序。
<div id="box"style="display:inline-block;padding:25px;background-color:lightgreen;">
<div id="boxIn" style="height: 50px;width: 50px;background-color:pink;">123</div>
</div>
<button id="btn1">内容为123的div元素获取焦点</button>
<button id="btn2">内容为123的div元素失去焦点</button>
<button id="reset">还原</button>
<script>
reset.onclick = function(){history.go();}
//focus()方法
btn1.onclick = function(){
boxIn.tabIndex= -1;
boxIn.focus();
}
//blur()方法
btn2.onclick = function(){
boxIn.blur();
}
//focusin事件
if(boxIn.addEventListener){
boxIn.addEventListener('focusin',handler)
}else{
boxIn.onfocusin = handler;
}
function handler(){
this.style.backgroundColor ='lightblue';
}
if(box.addEventListener){
box.addEventListener('focusin',handler)
}else{
box.onfocusin = handler;
}
//blur事件
function fnBlur(){
this.style.backgroundColor = 'orange';
}
boxIn.onblur = fnBlur;
box.onblur = fnBlur;
</script>
由运行结果可知,focusin 事件可冒泡;而 blur 事件不可冒泡。
焦点事件常用于表单展示及验证。
比如,获取焦点时,修改背景颜色;失去焦点时,还原背景颜色并验证。
<div id="box">
<input id="input1" type="text" placeholder="只可以输入数字">
<input id="input2" type="text" placeholder="只可以输入汉字">
<span id="tips"></span>
</div>
<script>
if(box.addEventListener){
box.addEventListener('focusin',fnIn);
box.addEventListener('focusout',fnOut);
}else{
box.onfocusin = fnIn;
box.onfocusout = fnOut;
}
function fnIn(e){
e = e || event;
var target = e.target || e.srcElement;
target.style.backgroundColor = 'lightgreen';
}
function fnOut(e){
e = e || event;
var target = e.target || e.srcElement;
target.style.backgroundColor = 'initial';
//如果是验证数字的文本框
if(target === input1){
if(!/^\d*$/.test(target.value.trim())){
target.focus();
tips.innerHTML = '只能输入数字,请重新输入'
setTimeout(function(){
tips.innerHTML = ''
},500);
}
}
//如果是验证汉字的文本框
if(target === input2){
if(!/^[\u4e00-\u9fa5]*$/.test(target.value.trim())){
target.focus();
tips.innerHTML = '只能输入汉字,请重新输入'
setTimeout(function(){
tips.innerHTML = ''
},500);
}
}
}
</script>
总结
以上就是为大家总结 JavaScript 中焦点管理的全部内容,这篇文章介绍的很详细,对大家的学习和工作具有一定的参考借鉴价值,如果有疑问大家可以留言交流。
JS怎么实现页面强制刷新
2026-11-05 14:53:38
点击JS链接直接运行不下载?前端实现.js文件强制下载的三种方法
2026-11-05 12:12:23
怎么封装一个通用的JS创建元素辅助函数
2026-11-05 11:41:41
Swiper.js 怎么监听滑动结束并获取当前索引
2026-11-05 10:57:15
HTML、CSS和JS的关系:Web前端三件套怎么协作
2026-11-04 15:41:40
