使用此处提供的解决方案 How to highlight active page in a masterpage menu?我能够让我的事件菜单项正确显示在导航菜单上。我正在使用带有母版页的 VB.NET。以下代码位于母版页中:
<script>
function equalHeight(group) {
tallest = 0;
group.each(function () {
thisHeight = $(this).height();
if (thisHeight > tallest) {
tallest = thisHeight;
}
});
group.each(function () { $(this).height(tallest); });
}
jQuery(document).ready(function () {
App.init();
// Call to set active menu selecttion
var str = location.href.toLowerCase();
$(".nav li a").each(function () {
if (str.indexOf($(this).attr("href").toLowerCase()) > -1) {
$("li.active").removeClass("active");
$(this).parent().addClass("active");
}
});
equalHeight($(".img-thumbnail"));
});
</script>
添加:如果一个部分(例如“工具”)下有多个页面,您能想出任何方法让上面的代码选择正确的事件菜单项吗?
最佳答案
使用这个 Javascript
function LoadActiveMenu()
{
var str=location.href.toLowerCase();
$("li a").each(function() {
if (str.indexOf($(this).attr("href").toLowerCase()) > -1) {
$("li.active").removeClass("active");
$(this).parent().addClass("active");
}
});
$("li.active").parents().each(function(){
if ($(this).is("li")){
$(this).addClass("active");
}
});
}
然后像下面这样在 body load 事件上调用这个函数
<body onload="LoadActiveMenu();">
</body>
关于jquery - 突出显示事件菜单选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21035446/