jquery實(shí)現(xiàn)tab鍵進(jìn)行選擇后enter鍵觸發(fā)click行為
更新時(shí)間:2017年03月29日 09:58:55 作者:cl296894988
本文主要介紹了jquery鍵盤事件實(shí)現(xiàn)tab鍵進(jìn)行選擇后enter鍵觸發(fā)click行為的方法,這種使用場景為當(dāng)首頁有幾個(gè)鏈接需要選擇的時(shí)候,使用鍵盤就可以進(jìn)行觸發(fā)行為。下面跟著小編一起來看下吧
這種使用場景為當(dāng)首頁有幾個(gè)鏈接需要選擇的時(shí)候,使用鍵盤就可以進(jìn)行觸發(fā)行為
復(fù)制下來放本地用吧 網(wǎng)頁上直接測試有問題
效果圖:

下面是demo代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>9</title>
<script type="text/javascript" src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script>
<style type="text/css">
.active{
background: pink;
}
</style>
</head>
<body>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="aa(111)">111111111111111111</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="aa(2222)">222222222222222222</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="aa(3333)">333333333333333333333</a>
<a class="active" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="aa(44444)">4444444444444444444444</a>
<!-- <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >111111111111111</a> -->
<!-- <script type="text/javascript">
$("body").on("click",function(){
var $active=$(".active");
var index=$active.index();
var totalLength=$("div").length;
if (index==totalLength-1) {
$($("div")[0]).addClass("active").siblings("div").removeClass("active");
}else{
$active.next().addClass("active").siblings("div").removeClass("active");
}
})
</script> -->
<script type="text/javascript">
document.onkeydown=function(event){
var e = event || window.event || arguments.callee.caller.arguments[0];
if(e && e.keyCode==9){
// console.log(9999);
var $active=$(".active");
var index=$active.index();
var totalLength=$("a").length;
if (index==totalLength-1) {
$($("a")[0]).addClass("active").siblings("a").removeClass("active");
}else{
$active.next().addClass("active").siblings("a").removeClass("active");
}
return false;
}
if(e && e.keyCode==13){ // enter 鍵
var $active=$(".active");
// var aa=$active.value;
// $active.click(function(event) {
// /* Act on the event */
// });
$active.trigger("click");
// console.log(aa);
// console.log(9999);
}
};
</script>
<!-- <script type="text/javascript">
var $active=$(".active");
var aa=$active.value;
console.log(aa);
</script> -->
<script type="text/javascript">
function aa(ss){
alert(ss);
}
</script>
<!-- <script type="text/javascript">
var arr=[1,2,3];
var index = Math.floor((Math.random()*arr.length));
console.log(arr[index]);
</script> -->
</body>
</html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
您可能感興趣的文章:
- 按Enter鍵觸發(fā)事件的jquery方法實(shí)現(xiàn)代碼
- jquery實(shí)現(xiàn)按Enter鍵觸發(fā)事件示例
- 基于jquery的button默認(rèn)enter事件(回車事件)。
- jquery 按鍵盤上的enter事件
- jQuery事件之鍵盤事件(ctrl+Enter回車鍵提交表單等)
- 修改jQuery.Autocomplete插件 支持中文輸入法 避免TAB、ENTER鍵失效、導(dǎo)致表單提交
- 基于Jquery的實(shí)現(xiàn)回車鍵Enter切換焦點(diǎn)
- Jquery利用mouseenter和mouseleave實(shí)現(xiàn)鼠標(biāo)經(jīng)過彈出層且可以點(diǎn)擊
- 基于Jquery的回車成tab焦點(diǎn)切換效果代碼(Enter To Tab )
- jQuery響應(yīng)enter鍵的實(shí)現(xiàn)思路
相關(guān)文章
jQuery實(shí)現(xiàn)動(dòng)態(tài)添加標(biāo)簽事件
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)動(dòng)態(tài)添加標(biāo)簽事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
基于jQuery實(shí)現(xiàn)表單提交驗(yàn)證
本文給大家分享的是一段基于jQuery的實(shí)現(xiàn)簡單的表單提交驗(yàn)證的代碼,非常簡單實(shí)用,推薦給有相同需求的小伙伴們參考下。2014-11-11
利用jQuery實(shí)現(xiàn)滑動(dòng)開關(guān)按鈕效果(附demo源碼下載)
這篇文章主要介紹了利用jQuery實(shí)現(xiàn)滑動(dòng)開關(guān)按鈕效果,文中給出了詳細(xì)的介紹,并在文末給出了完整的demo源碼下載,有需要的朋友可以參考簡介,下面來一起看看吧。2017-02-02

