gpt4 book ai didi

javascript - 通过子导航(焦点功能)的 JS/JQuery keydown 循环无法按预期工作

转载 作者:行者123 更新时间:2023-11-27 22:50:09 25 4
gpt4 key购买 nike

为了实现可访问性,我需要在导航上具有扩展焦点功能。在焦点上,子导航显示,如果按下向下或向上键,jquery 将在保持焦点的同时循环浏览子导航中的项目。我几乎已经解决了这个问题,但是,jquery next() 没有按预期工作,而是跳过两个或者有时只是按随机顺序进行。

HTML:-

<nav class="nav nav--primary" role="navigation">

<a href="/" title="Home" class="primary-nav-link parent active" data-position="1" tabindex="1">wasd</a>

<div class="nav__dropdown">
<a class="primary-nav-link parent " tabindex="1">wasd</a>
<span class="nav__expand"></span>
<div class="nav__dropdown__content" aria-hidden="true">
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.1">wasd</a>
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.2">wasd</a>
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.3">wasd</a>
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.4">wasd</a>
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.5">wasd</a>
</div>
</div>

<div class="nav__dropdown">
<a class="primary-nav-link parent " tabindex="1">wasd</a>
<span class="nav__expand"></span>
<div class="nav__dropdown__content" aria-hidden="true">
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.1">wasd</a>
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.2">wasd</a>
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.3">wasd</a>
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.4">wasd</a>
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.5">wasd</a>
</div>
</div>

<div class="nav__dropdown">
<a class="primary-nav-link parent " tabindex="1">wasd</a>
<span class="nav__expand"></span>
<div class="nav__dropdown__content" aria-hidden="true">
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.1">wasd</a>
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.2">wasd</a>
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.3">wasd</a>
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.4">wasd</a>
<a href="#" class="sub-nav-link" tabindex="-1" data-position="3.5">wasd</a>
</div>
</div>

</nav>

JS:-

   $(".parent").focus(function(){

$(this).closest(".nav__dropdown").addClass("hover");
$('.hover .nav__dropdown__content a').first().addClass('navSelected');

$(document).keydown(function(e) {
var selected = $('.navSelected');
var sub = $('.hover .nav__dropdown__content a');


if(e.which === 40){

if(selected){
selected.removeClass('navSelected');
next = selected.next();
if(next.length > 0){
selected = next.addClass('navSelected');
}else{
selected = sub.eq(0).addClass('navSelected');
}
}else{
selected = sub.eq(0).addClass('navSelected');
}


}else if(e.which === 38){
if(selected){
selected.removeClass('navSelected');
next = selected.prev();
if(next.length > 0){
selected = next.addClass('navSelected');
}else{
selected = sub.last().addClass('navSelected');
}
}else{
selected = sub.last().addClass('navSelected');
}
}
});

}).blur(function(){
$(".nav__dropdown").removeClass("hover");
$(document).removeClass("navSelected");
})

工作 js fiddle :-

https://jsfiddle.net/zpnfzevb/

最佳答案

绑定(bind)到 $(document) 的 keydown 事件导致额外的循环,这意味着选择将最终出现在不同的位置。

该事件现在绑定(bind)到 $('nav') 并在模糊函数中取消绑定(bind),这已删除了任何不需要的触发器,现在的行为符合预期。

最终 JS 如下:-

  $(".parent").focus(function(){

$(this).closest(".nav__dropdown").addClass("hover");
$('.hover .nav__dropdown__content a').first().addClass('navSelected');

$('nav').keydown(function(e) {
var selected = $('.navSelected');
var sub = $('.hover .nav__dropdown__content a');


if(e.which === 40){

if(selected){
selected.removeClass('navSelected');
next = selected.next();
if(next.length > 0){
selected = next.addClass('navSelected');
}else{
selected = sub.eq(0).addClass('navSelected');
}
}


}
if(e.which === 38){
if(selected){
selected.removeClass('navSelected');
next = selected.prev();
if(next.length > 0){
selected = next.addClass('navSelected');
}else{
selected = sub.last().addClass('navSelected');
}
}
}

if(e.which === 13){
if(selected){
selected.trigger('click');
}
}

})

}).blur(function(){
$('nav').unbind('keydown');
$(".nav__dropdown").removeClass("hover");
$(document).removeClass("navSelected");
})

关于javascript - 通过子导航(焦点功能)的 JS/JQuery keydown 循环无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38124821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com