我的菜单结构如下。
$('html').removeClass('no-js');
// Add plus mark to li that have a sub menu
$('li:has("ul") > a').append('<span class="plusMark">+</span>');
Mousetrap.bind('down', function(e) {
if ($(".manu-t").children("ul").children("li").children(":focus").length != 0) {
//mentioned below What I have tried till now
}
});
#nav,
#nav ul,
#nav li {
margin: 0;
padding: 0;
border: 0;
list-style: none;
box-sizing: border-box;
}
#nav {
position: relative;
min-height: 30px;
max-width: 100%;
background-color: #b5b5b5;
color: #000;
}
#nav li {
position: relative;
}
#nav a {
text-decoration: none;
height: 100%;
display: block;
padding: 0 20px;
}
#nav>ul,
.fa {
height: 30px;
line-height: 30px;
}
#nav>ul>li {
position: relative;
text-align: center;
}
#nav>ul>li>a {
background-color: #b5b5b5;
}
#nav>ul>li>a:hover,
#nav>ul>li>a:focus,
#nav>ul>li>a.js-openSubMenu {
background-color: #5f5f5f;
}
#nav>ul>li:hover>a,
#nav>ul>li:focus>a {
background-color: #5f5f5f;
color: #fff;
}
#nav>ul>li>ul>li>a {
background-color: #5f5f5f;
}
#nav>ul>li>ul>li>a:hover,
#nav>ul>li>ul>li>a:focus {
background-color: #b5b5b5;
}
#nav>ul>li>ul>li:not(:last-child) a {
border-bottom: 1px solid #b5b5b5;
}
#nav>ul>li>ul>li>ul>li>a {
background-color: #b5b5b5;
}
#nav>ul>li>ul>li>ul>li>a:hover,
#nav>ul>li>ul>li>ul>li>a:focus {
background-color: #5f5f5f;
}
#nav>ul>li>ul>li>ul>li:not(:last-child)>a {
border-bottom: 1px solid #5f5f5f;
}
/* Javascript classes */
#nav .js-hideElement {
display: none;
}
#nav .js-showElement {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="containe-header">
<nav id="nav">
<ul>
<li class="menu-t"><a href="#">Main</a>
<ul>
<li><a href="#">Item1</a>
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
</ul>
</li>
<li><a href="#">Item2</a>
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
<li><a href="#">Sub Item 5</a></li>
<li><a href="#">Sub Item 6</a></li>
</ul>
</li>
<li><a href="#">Item3</a></li>
<li><a href="#">Item4</a></li>
</ul>
</li>
<li class="menu-l"><a href="#">Menu2</a></li>
</ul>
</nav>
</div>
问题是将焦点从Item1移至Item2,再移至Item3,依此类推...通过按下箭头键(我已使用Mousetrap库检测按键)
到目前为止我尝试过的。
1。
var focused_index = 1;
focused_index=focused_index - 1;
$('a').eq(focused_index).focus();
但是此方法着重于嵌套元素的意思,即从item1到Sub Item 1,再到Sub Item 2,依此类推。
2。
// Get the focused element:
var $focused = $(':focus');
// No jQuery:
var focused = document.activeElement;
// Does the element have focus:
var hasFocus = $('foo').is(':focus');
// No jQuery:
elem === elem.ownerDocument.activeElement;
但这会导致在Body标签内具有其他焦点的其他元素出现问题。
所以我如何将焦点从Item1移到Item2。
答案 0 :(得分:1)
我想关注Item2的元素。然后转到Item3的元素。
您可以使用:
$("a:focus").closest("li").next().find("a").first().focus();
工作片段:
// Add plus mark to li that have a sub menu
$('li:has("ul") > a').append('<span class="plusMark">+</span>');
// set initial focus
$("#nav>ul>li.menu-t>ul>li>a:first()").focus();
$(document).on("keydown", function(e) {
if (e.keyCode == 40) {
$("a:focus").closest("li").next().find("a").first().focus();
return false;
}
if (e.keyCode == 38) {
$("a:focus").closest("li").prev().find("a").first().focus();
return false;
}
//if ($(".manu-t").children("ul").children("li").children(":focus").length != 0) {
//mentioned below What I have tried till now
//}
});
a { text-decoration: none; }
:focus { background-color: yellow; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="containe-header">
<nav id="nav">
<ul>
<li class="menu-t"><a href="#">Main</a>
<ul>
<li><a href="#">Item1</a>
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
</ul>
</li>
<li><a href="#">Item2</a>
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
<li><a href="#">Sub Item 5</a></li>
<li><a href="#">Sub Item 6</a></li>
</ul>
</li>
<li><a href="#">Item3</a></li>
<li><a href="#">Item4</a></li>
</ul>
</li>
<li class="menu-l"><a href="#">Menu2</a></li>
</ul>
</nav>
</div>
答案 1 :(得分:0)
您可以使用CSS选择器将要“关注”的元素存储在数组中。为“ keydown”事件添加事件处理程序-如果name
显示用户单击了向下箭头,则迭代到数组中的下一个元素。我通过使用一个生成器函数来做到这一点,当我们到达数组的末尾时,该函数将旋转回到e.code
索引:
0
function* nextElGenerator() {
var liEls = document.querySelectorAll('.menu-t > ul > li > a:first-child');
var i = 0;
while (true) {
yield liEls[i];
i += 1;
if (i === liEls.length) {
i = 0;
}
}
}
const bulletElItr = nextElGenerator();
let curEl;
document.addEventListener('keydown', e => {
if (e.code === 'ArrowDown') {
if (curEl) {
curEl.style.backgroundColor = 'transparent';
}
curEl = bulletElItr.next().value;
curEl.style.backgroundColor = 'yellow';
}
});