最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳細(xì)總結(jié)Javascript中的焦點(diǎn)管理

 更新時(shí)間:2016年09月17日 08:39:39   投稿:daisy  
相信大家都知道焦點(diǎn)作為javascript中的一個(gè)重要功能,基本上和頁(yè)面交互都離不開(kāi)焦點(diǎn)。但卻少有人對(duì)焦點(diǎn)管理系統(tǒng)地做總結(jié)歸納。本文就javascript中的焦點(diǎn)管理作詳細(xì)介紹,有需要的朋友們可以參考借鑒。

焦點(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種,包括頁(yè)面加載、用戶(hù)輸入(按tab鍵)、focus()方法和autofocus屬性

【1】頁(yè)面加載

默認(rèn)情況下,文檔剛剛加載完成時(shí),document.activeElement中保存的是body元素的引用。文檔加載期間,document.activeElement的值為null

<script>
console.log(document.activeElement);//null
</script>
<body>
<script>
console.log(document.activeElement);//<body>
</script>
</body>

【2】用戶(hù)輸入(按tab鍵)

用戶(hù)通??梢允褂胻ab鍵移動(dòng)焦點(diǎn),使用空格鍵激活焦點(diǎn)。比如,如果焦點(diǎn)在一個(gè)鏈接上,此時(shí)按一下空格鍵,就會(huì)跳轉(zhuǎn)到該鏈接

說(shuō)到tab鍵,就不能不提到tabindex屬性。tabindex屬性用來(lái)指定當(dāng)前HTML元素節(jié)點(diǎn)是否被tab鍵遍歷,以及遍歷的優(yōu)先級(jí)

1、如果tabindex=-1,tab鍵跳過(guò)當(dāng)前元素

2、如果tabindex=0,表示tab鍵將遍歷當(dāng)前元素。如果一個(gè)元素沒(méi)有設(shè)置tabindex,默認(rèn)值就是0

3、如果tabindex大于0,表示tab鍵優(yōu)先遍歷。值越大,就表示優(yōu)先級(jí)越小

下列代碼中,使用tab鍵時(shí),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)鍵盤(pán)事件

[注意]前面介紹過(guò),若非表單元素,設(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表單字段新增了一個(gè)autofocus屬性,只要設(shè)置這個(gè)屬性, 不用javascript就能自動(dòng)把焦點(diǎn)移動(dòng)到相應(yīng)字段

[注意]該屬性只能用于表單元素,普通元素即使設(shè)置tabIndex=”-1″也不生效

<input autofocus value="abc">

hasFocus()

document.hasFocus()方法返回一個(gè)布爾值,表示當(dāng)前文檔之中是否有元素被激活或獲得焦點(diǎn)。通過(guò)檢測(cè)文檔是否獲得了焦點(diǎn),可以知道是不是正在與頁(yè)面交互

console.log(document.hasFocus());//true
//在兩秒鐘內(nèi)點(diǎn)擊其他標(biāo)簽頁(yè),使焦點(diǎn)離開(kāi)當(dāng)前頁(yè)面
setTimeout(function(){
 console.log(document.hasFocus());//false
},2000);

失去焦點(diǎn)

如果使用javascript來(lái)使元素失去焦點(diǎn),那么就要使用blur()方法

blur()方法的作用是從元素中移走焦點(diǎn)。在調(diào)用blur()方法時(shí),并不會(huì)把焦點(diǎn)轉(zhuǎn)移到某個(gè)特定的元素上;僅僅是將焦點(diǎn)從調(diào)用這個(gè)方法的元素上面移走而已

<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)事件會(huì)在頁(yè)面獲得或失去焦點(diǎn)時(shí)觸發(fā)。利用這些事件并與document.hasFocus()方法及 document.activeElement屬性配合,可以知曉用戶(hù)在頁(yè)面上的行蹤

焦點(diǎn)事件共包括下面4個(gè)

1、blur

blur事件在元素失去焦點(diǎn)時(shí)觸發(fā)。這個(gè)事件不會(huì)冒泡

2、focus

focus事件在元素獲得焦點(diǎn)時(shí)觸發(fā)。這個(gè)事件不會(huì)冒泡

3、focusin

focusin事件在元素獲得焦點(diǎn)時(shí)觸發(fā)。這個(gè)事件與focus事件等價(jià),但它冒泡

4、focusout

focusour事件在元素失去焦點(diǎn)時(shí)觸發(fā)。這個(gè)事件與blur事件等價(jià),但它冒泡

[注意]關(guān)于focusin和focusout事件,除了IE瀏覽器支持DOM0級(jí)事件處理程序,其他瀏覽器都只支持DOM2級(jí)事件處理程序

<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)時(shí),修改背景顏色;失去焦點(diǎn)時(shí),還原背景顏色并驗(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ù)字,請(qǐng)重新輸入'
 setTimeout(function(){
 tips.innerHTML = ''
 },500);
 }
 }
 //如果是驗(yàn)證漢字的文本框
 if(target === input2){
 if(!/^[\u4e00-\u9fa5]*$/.test(target.value.trim())){
 target.focus();
 tips.innerHTML = '只能輸入漢字,請(qǐng)重新輸入'
 setTimeout(function(){
 tips.innerHTML = ''
 },500);
 }
 } 
}
</script>

總結(jié)

以上就是為大家總結(jié)Javascript中焦點(diǎn)管理的全部?jī)?nèi)容,這篇文章介紹的很詳細(xì),對(duì)大家的學(xué)習(xí)和工作具有一定的參考借鑒價(jià)值,如果有疑問(wèn)大家可以留言交流。

相關(guān)文章

  • JS學(xué)習(xí)筆記之?dāng)?shù)組去重實(shí)現(xiàn)方法小結(jié)

    JS學(xué)習(xí)筆記之?dāng)?shù)組去重實(shí)現(xiàn)方法小結(jié)

    這篇文章主要介紹了JS學(xué)習(xí)筆記之?dāng)?shù)組去重實(shí)現(xiàn)方法,結(jié)合實(shí)例形式總結(jié)分析了javascript數(shù)組去重的5種常見(jiàn)實(shí)現(xiàn)方法及相關(guān)操作技巧,需要的朋友可以參考下
    2019-05-05
  • JS查看對(duì)象功能代碼

    JS查看對(duì)象功能代碼

    通過(guò)JS查看到一個(gè)頁(yè)面有些什么對(duì)象的實(shí)現(xiàn)代碼
    2008-04-04
  • 使用JavaScript實(shí)現(xiàn)表格編輯器(實(shí)例講解)

    使用JavaScript實(shí)現(xiàn)表格編輯器(實(shí)例講解)

    下面小編就為大家?guī)?lái)一篇使用JavaScript實(shí)現(xiàn)表格編輯器(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • 對(duì)JavaScript中this指針的新理解分享

    對(duì)JavaScript中this指針的新理解分享

    這篇文章主要介紹了對(duì)JavaScript中this指針的新理解分享,本文講解了方法調(diào)用模式、函數(shù)調(diào)用模式、構(gòu)造函數(shù)調(diào)用模式、Apply調(diào)用模式中的this指針理解,需要的朋友可以參考下
    2015-01-01
  • JS使用正則實(shí)現(xiàn)去掉字符串左右空格的方法

    JS使用正則實(shí)現(xiàn)去掉字符串左右空格的方法

    這篇文章主要介紹了JS使用正則實(shí)現(xiàn)去掉字符串左右空格的方法,結(jié)合實(shí)例形式分析了JS針對(duì)首尾匹配及空格匹配的簡(jiǎn)單實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-12-12
  • JavaScript中transform實(shí)現(xiàn)數(shù)字翻頁(yè)效果

    JavaScript中transform實(shí)現(xiàn)數(shù)字翻頁(yè)效果

    本文主要介紹JavaScript中利用transform實(shí)現(xiàn)數(shù)字翻頁(yè)效果的實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-03-03
  • javascript自定義右鍵菜單插件

    javascript自定義右鍵菜單插件

    這篇文章主要為大家詳細(xì)介紹了javascript自定義右鍵菜單插件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • js跨域問(wèn)題淺析及解決方法優(yōu)缺點(diǎn)對(duì)比

    js跨域問(wèn)題淺析及解決方法優(yōu)缺點(diǎn)對(duì)比

    所謂js跨域問(wèn)題,是指在一個(gè)域下的頁(yè)面中通過(guò)js訪(fǎng)問(wèn)另一個(gè)不同域下 的數(shù)據(jù)對(duì)象,出于安全性考 慮,幾乎所有瀏覽器都不允許這種跨域訪(fǎng)問(wèn),這就導(dǎo)致在一些ajax應(yīng)用中,使用跨域的web service會(huì)成為一個(gè)問(wèn)題。 要解決跨域的問(wèn)題,就是本文我們需要探討的了
    2014-11-11
  • 微信小程序商品詳情頁(yè)底部彈出框

    微信小程序商品詳情頁(yè)底部彈出框

    這篇文章主要為大家詳細(xì)介紹了微信小程序商品詳情頁(yè)底部彈出框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • bootstrap是什么_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    bootstrap是什么_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要介紹了bootstrap是什么,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07

最新評(píng)論

沁水县| 郓城县| 佛教| 东源县| 财经| 苍梧县| 南平市| 洛南县| 嫩江县| 河源市| 安岳县| 历史| 连云港市| 专栏| 苍南县| 大足县| 黄陵县| 芮城县| 柏乡县| 海城市| 万全县| 平谷区| 芮城县| 朔州市| 临潭县| 永德县| 德令哈市| 阿巴嘎旗| 隆尧县| 东乡县| 盐城市| 大安市| 新竹市| 林周县| 广昌县| 林芝县| 卢龙县| 常德市| 临邑县| 玛沁县| 大庆市|