I am working with a menu structure as shown below.
$('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>
The challenge is to shift focus from Item1 to Item2 and then to Item3 by pressing the down arrow key (using Mousetrap library for keypress detection)
This is what I have attempted so far...
1.
var focused_index = 1;
focused_index=focused_index - 1;
$('a').eq(focused_index).focus();
However, this method focuses on nested elements, moving from Item1 to Sub Item 1, then Sub Item 2, and so on...
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;
Nevertheless, this causes issues with other elements in the Body tag that have focus.
- Check out the answer to this question Send focus to dynamic li with jQuery
How can I successfully shift focus from Item1 to Item2?