find all links within a list with a specific class
I have a main menu:
<ul class="menu">
<li><a href="/"><span class="l"></span><span class="r"></span><span class="t">Home</span></a></li>
<li><a href="<%= Url.Action("Login", "User")%>"><span class="l"></span><span class="r"></span><span class="t">My Account</span></a></li>
<li><a href="#"><span class="l"></span><span class="r"></span><span class="t">Post Idea</span></a></li>
<li><a href="#"><span class="l"></span><span class="r"></span><span class="t">Browse Ideas</span></a></li>
<li><a href="#"><span class="l"></span><span class="r"></span><span class="t">Invest in Ideas</span></a></li>
And i am trying to select the active link using jQuery:
<script type="text/javascript">
$(document).ready(funct开发者_运维知识库ion () {
var path = location.pathname;
var home = "/";
$("a[href='" + [path || home] + "']").addClass("top-menuactive");
});
</script>
However the javascript depicted above finds all links on site, not just the links in the menu.
How can i change the script to select only the links within<ul class="menu">
You can use the descendant selector:
<script type="text/javascript">
$(document).ready(function () {
var path = location.pathname;
var home = "/";
$("ul.menu a[href='" + [path || home] + "']").addClass("top-menuactive");
});
</script>
精彩评论