详细总结Javascript中的焦点管理

焦点元素

到底哪些元素可以获得焦点呢?默认情况下,只有表单元素可以获得焦点。因为只有表单元素可以交互

<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

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()

document.hasFocus()方法返回一个布尔值,表示当前文档之中是否有元素被激活或获得焦点。通过检测文档是否获得了焦点,可以知道是不是正在与页面交互

console.log(document.hasFocus());//true
//在两秒钟内点击其他标签页,使焦点离开当前页面
setTimeout(function(){
 console.log(document.hasFocus());//false
},2000);

失去焦点

如果使用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中焦点管理的全部内容,这篇文章介绍的很详细,对大家的学习和工作具有一定的参考借鉴价值,如果有疑问大家可以留言交流。

(0)

相关推荐

  • 有效的捕获JavaScript焦点的方法小结

    1. 设置元素可获得焦点以监听键盘事件 元素聚焦最大好处就是可允许发送键盘事件,HTML很多元素默认就有可聚焦,如form表单元素,a锚链接等,但大部份默认是不能聚焦的.要使得元素能够聚焦,可以在HTML代码或JavaScript脚本中实现. html: 复制代码 代码如下: <div tabIndex="0" style="height:100px;width:100px; background:red;"></div> JavaScrip

  • 比较炫的图片播放器 js 焦点效果代码

    图片播放器_图片轮换_焦点效果 #focus_m{position:relative; width:420px; height:384px; background:#133775} .f_img_roll{width:350px; height:300px; position:relative;} .f_img_roll img{position:absolute; left:0; top:0; width:350px; height:300px;} .f_img_tree{position:a

  • JavaScript(js)设置默认输入焦点(focus)

    常常会在回复和引用里使用此功能,即单击回复或引用,如让输入焦点出现在留言输入框中,如果使用锚来定位,输入焦点就不能激活了. 复制代码 代码如下: javascript:document.getElementById("id").focus(); 或javascript:document.all.id.focus(); 或javascript:document.all.name.focus(); 例子: 复制代码 代码如下: <input type="text"

  • JavaScript 关于元素获取焦点(隐藏元素与div)

    1,隐藏元素无法获取焦点 2,对于div等特殊元素获取焦点 关于元素获取焦点 body { margin: 32px; font-family: Verdana, sans-serif; font-size: 14px; } .title { font-size: 18px; font-weight: bolder;margin:40px 0; } 关于元素获取焦点: 1,隐藏元素无法获取焦点 下面有个隐藏的Input: 解决方法:先把元素显示,在获取焦点. 2,对于div等特殊元素获取焦点 我

  • JS焦点图切换,上下翻转

    复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&qu

  • js 获取坐标 通过JS得到当前焦点(鼠标)的坐标属性

    通过JS得到当前焦点的坐标 如下是得到当前焦点的坐标: 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head

  • 鼠标焦点离开文本框时验证的js代码

    利用js来验证文本框的值 复制代码 代码如下: <script> function onblurs(){ if(frm.name.value==""){ alert("请输入您的名字!"); }else if(frm.funny.value==""){ alert("爱好不得为空哦!"); } } </script> <form name="frm"> <tr>

  • 使用JS取得焦点(focus)元素代码

    对于良好的用户体验来说,网站/web app的可访问性和可用性,以及功能 都是至关重要的. 当我们的网站运行良好/体验很好的时候,用户是意识不到的,但我们做得不好时他们肯定会感觉到. 应用程序的可用性和可访问性的一个重要组成部分是输入焦点(focus)的处理,但这又是开发人员常常会忽视的一点. 对输入焦点处理很差的一个例子: 在点击一个链接以后打开一个窗口,但却不将光标聚焦到窗口中的任何元素内. 甚至更糟的是: 聚焦到模态窗口中的某个元素,但在关闭以后焦点照样不返回. 理想情况下,在触发链接时会

  • 获取焦点时,利用js定时器设定时间执行动作

    进入正题,先说说定时器. 在javascritp中,有两个关于定时器的专用函数,分别为: 1.倒计定时器:timename=setTimeout("function();",delaytime); 2.循环定时器:timename=setInterval("function();",delaytime); 第一个参数"function()"是定时器触发时要执行的动作,可以是一个函数,也可以是几个函数,函数间用":"隔开即可.比

  • 在js(jquery)中获得文本框焦点和失去焦点的方法

    先来看javascript的直接写在了input上 复制代码 代码如下: <input name="pwuser" type="text" id="pwuser" class="input" value="楼盘账号" onBlur="if(this.value=='') this.value='楼盘账号';" onFocus="if(this.value=='楼盘账号')

随机推荐