admin管理员组文章数量:1405120
Is this possible? My HTML looks like this:
<ul>
<li> <a> link </a> </li>
<li> <a> link </a> </li>
<li> <a> link </a>
<ul>
<li> <a> sub-link </a> </li>
<li> <a> sub-link </a> </li>
<li> <a> sub-link </a> </li>
</ul>
</li>
</ul>
Basically I want to trigger the hover CSS rule on the parent menu link when the mouse is over a child menu link.
Is this possible? My HTML looks like this:
<ul>
<li> <a> link </a> </li>
<li> <a> link </a> </li>
<li> <a> link </a>
<ul>
<li> <a> sub-link </a> </li>
<li> <a> sub-link </a> </li>
<li> <a> sub-link </a> </li>
</ul>
</li>
</ul>
Basically I want to trigger the hover CSS rule on the parent menu link when the mouse is over a child menu link.
Share Improve this question edited Aug 29, 2010 at 23:03 Gert Grenander 17.1k6 gold badges41 silver badges43 bronze badges asked Aug 29, 2010 at 22:50 AlexAlex 66.2k185 gold badges460 silver badges651 bronze badges 1-
a solution to this would be to move the hover styles on the
<li>
element, but I'm not sure it's a good idea to have :hover on li's instead of a's... – Alex Commented Aug 29, 2010 at 22:52
2 Answers
Reset to default 5If you use .hover()
it'll affect the parent as well, since that's how the mouseenter
and mouseleave
events work, for example:
$("li").hover(function() {
$(this).toggleClass("active");
});
You can give it a try here, unlike mouseover
and mouseout
, the events don't fire when entering/leaving children, so the action taken on the parent isn't "undone" until you actually leave the parent as well, which seems to be what you're after.
Or, use pure CSS if you're just doing styling, like this (doesn't work in IE6):
li:hover > a { color: red; }
You can test it here.
You can reach the parent node by using parent() method and toggle its class binding the mouseover and mouseout events.
$("#childnode").bind("mouseover", function() {
$(this).parent().addClass("onmouseover");
}).bind("mouseout", function() {
$(this).parent().removeClass("onmouseover");
});
本文标签: jQueryJavaScriptTrigger hover on a element when mouse over on another elementStack Overflow
版权声明:本文标题:jQueryJavaScript - Trigger hover on a element when mouse over on another element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744879255a2630106.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论