jQuery jquery选择下一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7222832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery select next element
提问by danyg
I have the next menu structure.
我有下一个菜单结构。
<li class="menu-422"><a href="item-1" title="">Item 1</a></li>
<li class="menu-444"><a href="item-2" title="">Item 2</a></li>
<li class="menu-449"><a href="item-3" title="">Item 3</a></li>
<li class="menu-452"><a href="item-4" title="">Item 4</a></li>
In this structure the items (the a-tag) have background. I want to change the background of the next item on hover event. If I'm over the Item 2 I want to change the background of the Item 3. I tried it with jQuery, but I didn't find the appropriate code.
在这个结构中,项目(a 标签)有背景。我想在悬停事件上更改下一个项目的背景。如果我超过了第 2 项,我想更改第 3 项的背景。我用 jQuery 尝试过,但没有找到合适的代码。
jQuery("#mymenu li a").hover(function() {
jQuery(this).next("li a").css('background', 'none');
}
I tried it with nextAll, with full selector path, but I can't select the next element.
我用 nextAll 尝试了它,带有完整的选择器路径,但我无法选择下一个元素。
回答by Joseph Silber
Upon hover, you want to go up to the parent li
, then use next()
to get to the next li
, then find('a')
inside of that li
:
悬停时,您想上到 parent li
,然后使用next()
到 next li
,然后find('a')
在里面li
:
$("#mymenu li a").hover(function() {
$(this).parent().next().find("a").css('background', 'none');
});
回答by ShankarSangoli
Instead of selecting the anchors you can just select li
and attach hover
event on it. And in the hover just use next
method to get the next li
element and change the background. I am assuming you dont have background set to the anchor's because I am changing the background of the li element.
您可以选择li
并附加hover
事件,而不是选择锚点。在悬停中,只需使用next
方法获取下一个li
元素并更改背景。我假设您没有将背景设置为锚点,因为我正在更改 li 元素的背景。
$("#mymenu li").hover(function() {
$(this).next().css('background', 'none');
//If you want to change the background of anchor try this
//$(this).next().find('a').css('background', 'none');
}