开发者

Adding 'active' class to the li which has a name similar to the one of the page you are on

开发者 https://www.devze.com 2023-02-18 17:17 出处:网络
<script> $(document).ready(function() { // store url for current page as global variable current_page = document.location.href
<script>    
$(document).ready(function() {
// store url for current page as global variable
current_page = document.location.href
// apply selected states depending on current page
if (current_page.match(/home/)) {
$("#krymson_nav ul li:eq(0)").addClass('active');
} else if (current_page.match(/dashboard/)) {
$("#krymson_nav ul li:eq(1)").addClass('active');
} else if (current_page.match(/content/)) {
$("#krymson_nav ul li:eq(2)").addClass('active');
} else if (current_page.match(/modules/)) {
} else { // don't mark any nav links as selected
$("#krymson_nav ul li")开发者_如何学运维.removeClass('selected');
};
});
</script>`

HTML:

<div id="krymson_nav">
<ul>
<li class="active"><a href="http://blackboarddev.com/new/modules/krymson/dashboard.php">Dashboard</a></li>
<li><a href="http://blackboarddev.com/new/modules/krymson/content.php">Content</a></li>
</ul>
</div>


In one line:

$("a[href='" + document.location.href + "']").closest('li').addClass('active');
0

精彩评论

暂无评论...
验证码 换一张
取 消