<ol id="nav">
<li><a href="a.php" class="selected">A</a>
<ol class="child">
<li><a href="a-1.php">A-1</a></li>
</ol>
</li>
<li><a href="b.php">B</a>
<ol class="child">
<li><a href="b-1.php">B-1</a></li>
<li><a href="b-2.php">B-2</a></li>
</ol>
</li>
<li><a href="c.php">C</a>
<ol class="child">
<li><a href="c-1.php">c-1</a></li>
<li><a href="c-2.php">c-2</a></li>
<li><a href="c-3.php">c-3</a></li>
<li><a href="c-4.php">c-4</a></li>
</ol>
</li>
</ol>
When child menu selected, its parent menu is selected, too.
The code below works on child css but not parent’s.
var loc = 'c-3.php';
$("#nav a").each(function() {
$(this).removeClass("selected");
if (loc.indexOf($(this).attr("href")) != -1) {
$(this).addClass("selected");
$(this).parent().addClass("selected"); /* <=== doesn't work :( */
}
});
This one doesn’t work either…
$(this).closest('ol').parent().addClass("selected");
you are confusing about the parents, this works:http://jsfiddle.net/DfcdQ/7/
$(this).parent()refers to the<li><a href="c-3.php">c-3</a></li>, it is not refereing to the<a href="c.php">C</a>