jQuery 需要帮助来导航/突出显示复杂列表上的元素
这是我的 hmtl 列表。
<div class="bradius3 taggy-beans">
<div class="taggy-beans-search"><input type="text" id="search-tags" class="bradiusMax" style=""></div>
<div class="pajinate-content">
<ul class="pajinate">
<li class="page1" style="display: inline;">
<ul class="filtered-tags">
<li><a id="4e72858ccaa47ca608510000" class="bradiusMax bean" href="javascript:void(0)">hey</a>
</li>
</ul>
</li>
<li class="page1" style="display: inline;">
<ul class="filtered-tags">
<li>
<a id="4e72858ccaa47ca6082d0000" class="bradiusMax bean" href="javascript:void(0)">hi</a>
</li>
</ul>
</li>
<li class="page1" style="display: inline;">
<ul class="filtered-tags">
<li>
<a id="4e72858ccaa47ca6082d0000" class="bradiusMax bean" href="javascript:void(0)">jhon</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
我的js脚本。
$('#search-tags').live('keydown',function(e){
var $prev, $next, $current = $(".pajinate a.highlight");
if (e.which === 40 && !$current.length) {
$(".pajinate a:first").addClass("highlight");
} else if (e.which === 39) {
console.log($next);
$next = $current.next(".pajinate a");
if ($next.length) {
$current.removeClass("highlight");
$next.addClass("highlight");
}
} else if (e.which === 37) {
console.log($prev);
$prev = $current.prev(".pajinate a");
if ($prev.length) {
$current.removeClass("highlight");
$prev.addClass("highlight");
}
} else if (e.which === 38) {
$(".pajinate a").removeClass("highlight");
}
});
我想做的是通过上面的 html 列表中的 e.keyCode 进行导航,尝试突出显示 a.bean 元素,但脚本仅突出显示第一个 a.bean,然后左键或右键不会突出显示
任何建议的 元素?
这是脚本逻辑的示例: http://jsfiddle.net/WeNdW/
this is my hmtl list.
<div class="bradius3 taggy-beans">
<div class="taggy-beans-search"><input type="text" id="search-tags" class="bradiusMax" style=""></div>
<div class="pajinate-content">
<ul class="pajinate">
<li class="page1" style="display: inline;">
<ul class="filtered-tags">
<li><a id="4e72858ccaa47ca608510000" class="bradiusMax bean" href="javascript:void(0)">hey</a>
</li>
</ul>
</li>
<li class="page1" style="display: inline;">
<ul class="filtered-tags">
<li>
<a id="4e72858ccaa47ca6082d0000" class="bradiusMax bean" href="javascript:void(0)">hi</a>
</li>
</ul>
</li>
<li class="page1" style="display: inline;">
<ul class="filtered-tags">
<li>
<a id="4e72858ccaa47ca6082d0000" class="bradiusMax bean" href="javascript:void(0)">jhon</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
my js script.
$('#search-tags').live('keydown',function(e){
var $prev, $next, $current = $(".pajinate a.highlight");
if (e.which === 40 && !$current.length) {
$(".pajinate a:first").addClass("highlight");
} else if (e.which === 39) {
console.log($next);
$next = $current.next(".pajinate a");
if ($next.length) {
$current.removeClass("highlight");
$next.addClass("highlight");
}
} else if (e.which === 37) {
console.log($prev);
$prev = $current.prev(".pajinate a");
if ($prev.length) {
$current.removeClass("highlight");
$prev.addClass("highlight");
}
} else if (e.which === 38) {
$(".pajinate a").removeClass("highlight");
}
});
what i'm trying to do is to navigate by e.keyCode in the html list above, trying highlighting a.bean elements but the script highlights only the first a.bean, then key left or right doesn't highlights a elements
any suggest?
here is an example of the script logic: http://jsfiddle.net/WeNdW/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您尝试突出显示某个元素,则您在 DOM 树中的位置太深,
next
无法正常工作。next
获取下一个同级,而 a 没有。a 被突出显示并设置为
$current
,因此
$next
和$prev
需要在 DOM 树上向上移动几层才能到达真正的下一个 a因此,表示
parent().parent().parent()
的部分将在 DOM 结构中向上移动适当的数量。同样,children().children().children()
返回并突出显示 a。在这里摆弄
if you're trying to highlight a elements, you would be too deep in the DOM tree for
next
to work properly.next
gets the next sibling, and a doesn't have any.a is highlighted and is set to be
$current
so
$next
and$prev
need to go a few levels up the DOM tree in order to get to the true next aThus, the part that says
parent().parent().parent()
to go the appropriate amount up the DOM structure. Similarly,children().children().children()
to go back down and highlight the a.fiddle here