2

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.

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... Commented Aug 29, 2010 at 22:52

2 Answers 2

5

If 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.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi. I know, but I'm not talking about enabling mouse over events, I just want the :hover style to be applied to the parent link, even if the mouse is not over it.
@Alex - If you put the :hover on the <li> you can do it, like this: jsfiddle.net/nick_craver/6ncQR/1
oh nvm :D tx, i'll use li:hover
@Alex - Make sure you have the CSS like it is on the right in the demo...of you had an example page I could take a look.
thank you, actually all that i need was the ">" css thing. didnt knew about it :)
1

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");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.