Apologies for the basic question but I am trying to add a hover state to a navigation list and cannot seem to work out how to have a hover state over a child without affecting the parent <li>. The parent <li> is technically being hovered over too. I am aware that .add/removeClass() is more ideal but for my testing, it was just easier with .attr().
So far I have got:
I have a jsfiddle set up at http://jsfiddle.net/gSPkj/ but below is the code-
HTML-
<div id="sidebarNav">
<ul>
<li class="parent"><a href="page1.html">Page 1</a></li>
<li class="parent"><a href="page2.html">Page 2</a></li>
<li class="parent"><a href="page3.html">Page 3</a></li>
<li class="parent"><a href="page4.html">Page 4</a>
<ul>
<li class="child"><a href="subpage_of_4-2.html">Subpage of 4 - 1</a></li>
<li class="child"><a href="subpage_of_4-2.html">Subpage of 4 - 2</a></li>
</ul>
</li>
</ul>
jQuery –
$("#sidebarNav li.parent").hover(function(){
$(this).attr("style","background:#123de1;color:#eb9028;");
},function(){
$(this).attr("style","background:#fff;color:#000;");
});
$("#sidebarNav li.child").hover(function(){
$(this).attr("style","background:#123de1;color:#eb9028;");
$(this).parents(".parent").attr("style","background:#fff;color:#000;");
},function(){
$(this).attr("style","background:#fff;color:#000;");
});
It would be easier to target the anchors, and then find the closest li element to attach the styles. I’d do it like this:
FIDDLE