詳細(xì)總結(jié)Javascript中的焦點(diǎn)管理
焦點(diǎn)元素
到底哪些元素可以獲得焦點(diǎn)呢?默認(rèn)情況下,只有表單元素可以獲得焦點(diǎn)。因?yàn)橹挥斜韱卧乜梢越换?/p>
<input type="text" value="223">
讓非表單元素獲得焦點(diǎn)也是有辦法的,先將tabIndex
屬性設(shè)置為-1,再調(diào)用focus()
方法
<div id="test" style="height:30px;width:100px;background:lightgreen">div</div> <button id="btn">div元素獲得焦點(diǎn)</button> <script> btn.onclick = function(){ test.tabIndex = -1; test.focus(); } test.onfocus = function(){ this.style.background = 'pink'; } </script>
activeElement
document.activeElement
屬性用于管理DOM焦點(diǎn),保存著當(dāng)前獲得焦點(diǎn)的元素
[注意]該屬性IE瀏覽器不支持
<div id="test" style="height:30px;width:100px;background:lightgreen">div</div> <button id="btn">div元素獲得焦點(diǎn)</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>
獲得焦點(diǎn)
元素獲得焦點(diǎn)的方式有4種,包括頁面加載、用戶輸入(按tab鍵)、focus()
方法和autofocus
屬性
【1】頁面加載
默認(rèn)情況下,文檔剛剛加載完成時,document.activeElement
中保存的是body元素的引用。文檔加載期間,document.activeElement
的值為null
<script> console.log(document.activeElement);//null </script> <body> <script> console.log(document.activeElement);//<body> </script> </body>
【2】用戶輸入(按tab鍵)
用戶通??梢允褂胻ab鍵移動焦點(diǎn),使用空格鍵激活焦點(diǎn)。比如,如果焦點(diǎn)在一個鏈接上,此時按一下空格鍵,就會跳轉(zhuǎn)到該鏈接
說到tab鍵,就不能不提到tabindex
屬性。tabindex
屬性用來指定當(dāng)前HTML元素節(jié)點(diǎn)是否被tab鍵遍歷,以及遍歷的優(yōu)先級
1、如果tabindex=-1
,tab鍵跳過當(dāng)前元素
2、如果tabindex=0
,表示tab鍵將遍歷當(dāng)前元素。如果一個元素沒有設(shè)置tabindex
,默認(rèn)值就是0
3、如果tabindex
大于0,表示tab鍵優(yōu)先遍歷。值越大,就表示優(yōu)先級越小
下列代碼中,使用tab鍵時,button獲得焦點(diǎn)的順序是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()
方法用于將瀏覽器的焦點(diǎn)設(shè)置到表單字段,即激活表單字段,使其可以響應(yīng)鍵盤事件
[注意]前面介紹過,若非表單元素,設(shè)置為tabIndex
為-1,也可以獲取焦點(diǎn)
<span id="test1" style="height:30px;width:100px;">span</span> <input id="test2" value="input"> <button id="btn1">span元素獲得焦點(diǎn)</button> <button id="btn2">input元素獲得焦點(diǎn)</button> <script> btn1.onclick = function(){test1.tabIndex=-1;test1.focus();} btn2.onclick = function(){test2.focus();} </script>
【4】autofocus
HTML5表單字段新增了一個autofocus
屬性,只要設(shè)置這個屬性, 不用javascript就能自動把焦點(diǎn)移動到相應(yīng)字段
[注意]該屬性只能用于表單元素,普通元素即使設(shè)置tabIndex=”-1″
也不生效
<input autofocus value="abc">
hasFocus()
document.hasFocus()
方法返回一個布爾值,表示當(dāng)前文檔之中是否有元素被激活或獲得焦點(diǎn)。通過檢測文檔是否獲得了焦點(diǎn),可以知道是不是正在與頁面交互
console.log(document.hasFocus());//true //在兩秒鐘內(nèi)點(diǎn)擊其他標(biāo)簽頁,使焦點(diǎn)離開當(dāng)前頁面 setTimeout(function(){ console.log(document.hasFocus());//false },2000);
失去焦點(diǎn)
如果使用javascript來使元素失去焦點(diǎn),那么就要使用blur()
方法
blur()
方法的作用是從元素中移走焦點(diǎn)。在調(diào)用blur()
方法時,并不會把焦點(diǎn)轉(zhuǎn)移到某個特定的元素上;僅僅是將焦點(diǎn)從調(diào)用這個方法的元素上面移走而已
<input id="test" type="text" value="123"> <button id="btn1">input元素獲得焦點(diǎn)</button> <button id="btn2">input元素失去焦點(diǎn)</button> <script> btn1.onclick = function(){test.focus();} btn2.onclick = function(){test.blur();} </script>
焦點(diǎn)事件
焦點(diǎn)事件會在頁面獲得或失去焦點(diǎn)時觸發(fā)。利用這些事件并與document.hasFocus()
方法及 document.activeElement
屬性配合,可以知曉用戶在頁面上的行蹤
焦點(diǎn)事件共包括下面4個
1、blur
blur
事件在元素失去焦點(diǎn)時觸發(fā)。這個事件不會冒泡
2、focus
focus
事件在元素獲得焦點(diǎn)時觸發(fā)。這個事件不會冒泡
3、focusin
focusin
事件在元素獲得焦點(diǎn)時觸發(fā)。這個事件與focus
事件等價,但它冒泡
4、focusout
focusour
事件在元素失去焦點(diǎn)時觸發(fā)。這個事件與blur事件等價,但它冒泡
[注意]關(guān)于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">內(nèi)容為123的div元素獲取焦點(diǎn)</button> <button id="btn2">內(nèi)容為123的div元素失去焦點(diǎn)</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>
由運(yùn)行結(jié)果可知,focusin
事件可冒泡;而blur
事件不可冒泡
焦點(diǎn)事件常用于表單展示及驗(yàn)證
比如,獲取焦點(diǎn)時,修改背景顏色;失去焦點(diǎn)時,還原背景顏色并驗(yàn)證
<div id="box"> <input id="input1" type="text" placeholder="只可以輸入數(shù)字"> <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'; //如果是驗(yàn)證數(shù)字的文本框 if(target === input1){ if(!/^\d*$/.test(target.value.trim())){ target.focus(); tips.innerHTML = '只能輸入數(shù)字,請重新輸入' setTimeout(function(){ tips.innerHTML = '' },500); } } //如果是驗(yàn)證漢字的文本框 if(target === input2){ if(!/^[\u4e00-\u9fa5]*$/.test(target.value.trim())){ target.focus(); tips.innerHTML = '只能輸入漢字,請重新輸入' setTimeout(function(){ tips.innerHTML = '' },500); } } } </script>
總結(jié)
以上就是為大家總結(jié)Javascript中焦點(diǎn)管理的全部內(nèi)容,這篇文章介紹的很詳細(xì),對大家的學(xué)習(xí)和工作具有一定的參考借鑒價值,如果有疑問大家可以留言交流。
- 在js(jquery)中獲得文本框焦點(diǎn)和失去焦點(diǎn)的方法
- JavaScript(js)設(shè)置默認(rèn)輸入焦點(diǎn)(focus)
- js 獲取坐標(biāo) 通過JS得到當(dāng)前焦點(diǎn)(鼠標(biāo))的坐標(biāo)屬性
- JS焦點(diǎn)圖切換,上下翻轉(zhuǎn)
- 使用JS取得焦點(diǎn)(focus)元素代碼
- 獲取焦點(diǎn)時,利用js定時器設(shè)定時間執(zhí)行動作
- 鼠標(biāo)焦點(diǎn)離開文本框時驗(yàn)證的js代碼
- 比較炫的圖片播放器 js 焦點(diǎn)效果代碼
- 有效的捕獲JavaScript焦點(diǎn)的方法小結(jié)
- JavaScript 關(guān)于元素獲取焦點(diǎn)(隱藏元素與div)
相關(guān)文章
Bootstrap 中下拉菜單修改成鼠標(biāo)懸停直接顯示
本文介紹將Bootstrap的二級菜單由點(diǎn)擊顯示改為鼠標(biāo)懸停顯示的具體實(shí)現(xiàn)方法,希望對大家有所幫助。2016-04-04JavaScript操作 url 中 search 部分方法函數(shù)
這篇文章主要介紹了JavaScript操作 url 中 search 部分方法函數(shù)的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06javascript實(shí)現(xiàn)簡約的頁面右下角點(diǎn)擊彈出窗口示例【測試可用】
這篇文章主要介紹了javascript實(shí)現(xiàn)的頁面右下角點(diǎn)擊彈出窗口功能,結(jié)合實(shí)例形式詳細(xì)分析了javascript頁面右下角點(diǎn)擊彈出窗口功能的相關(guān)步驟、原理與注意事項(xiàng),需要的朋友可以參考下2023-07-07詳解JavaScript中的鏈?zhǔn)秸{(diào)用
這篇文章主要介紹了JavaScript中的鏈?zhǔn)秸{(diào)用的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)JavaScript,感興趣的朋友可以了解下2020-11-11js和jquery中循環(huán)的退出和繼續(xù)下一個循環(huán)
退出循環(huán),使用break;退出當(dāng)前循環(huán)繼續(xù)下一個循環(huán),使用continue,jquery中使用return false;continue,使用return true2014-09-09Javascript浮點(diǎn)數(shù)乘積運(yùn)算出現(xiàn)多位小數(shù)的解決方法
這篇文章主要介紹了Javascript浮點(diǎn)數(shù)乘積運(yùn)算出現(xiàn)多位小數(shù)的解決方法,需要的朋友可以參考下2014-02-02