gpt4 book ai didi

javascript - 将焦点移至下一个 "li"元素,忽略所有嵌套元素

转载 作者:太空宇宙 更新时间:2023-11-04 06:26:33 25 4
gpt4 key购买 nike

我的菜单结构如下。

$('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 标签内有焦点。

  1. 这个问题的答案 Send focus to dynamic li with jQuery

那么我怎样才能将焦点从 Item1 移到 Item2。

最佳答案

I want to focus on element of Item2. and then to element of 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>

关于javascript - 将焦点移至下一个 "li"元素,忽略所有嵌套元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55047210/

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