gpt4 book ai didi

jquery - 为菜单 2 级添加事件,hiliting 2 级菜单 jquery

转载 作者:太空宇宙 更新时间:2023-11-04 03:05:21 24 4
gpt4 key购买 nike

我有一个 2 级菜单,当我单击第一级时,它以黄色突出显示并且工作正常。

但是,当我从第二级中选择一个类别时,我希望第一级中的主要类别也被突出显示,以便用户知道他正在浏览哪个主要类别。

这是我的网站: http://www.marseille-fitness.com/bodypump.php

<ul class="mainNav">
<li><a href="rpm.php">Cours collectifs</a>
<ul class="dropDown">
<li><a href="rpm.php">RPM</a></li>
<li><a href="bodypump.php">Bodypump</a></li>
<li><a href="bodyattack.php">Bodyattack</a></li>
<li><a href="bodycombat.php">Bodycombat</a></li>
<li><a href="bodyJam.php">Bodyjam</a></li>
<li><a href="bodybalance.php">Bodybalance</a></li>
<li><a href="cxworx.php">Cxworx</a></li>
</ul>
</li>
</ul>

<script>



$("ul.mainNav li").hover(function(){
$(this).find("ul.dropDown").css({"display" : "block"}).fadeTo('500', '1.0');
}, function() {
$(this).find("ul.dropDown").fadeTo('500', '0.0').css({"display" : "none"});
});
//class active in menu start
$(function(){

var url = window.location.pathname;
var urlRegExp = new RegExp(url.replace(/\/$/,'')); // create regexp to match current url pathname and remove trailing slash if present as it could collide with the link in navigation in case trailing slash wasn't present there

// now grab every link from the navigation
$('.mainNav li a').each(function(){
// and test its href against the url pathname regexp
if(urlRegExp.test(this.href)){
$(this).addClass('active');

}
});

});

</script>

CSS

    @import url(http://fonts.googleapis.com/css?family=Raleway:600,800);

ul.mainNav {
margin:0;
padding:0;
list-style-type:none;
background-color: #f0eff0;
height: 28px;
font-family: 'Raleway', sans-serif;
color: #606060;
float: right;
margin-right: 100px;

}
.menu-wrapper{
background-color: #f0eff0;
height: 3.4em;
}


.mainNav li {
margin:0;
padding:0;
list-style-type:none;
height: 28px;
line-height:28px;
float: left;
color: #606060;
font-size: 14px;
}
.mainNav li a {
padding: 0 20px;
line-height: 3.8em;
display:block;
color: #606060;
text-decoration:none;
font-family: 'Raleway', sans-serif;
font-weight: 800;
}

.mainNav li:hover > a {
background: #ffffff;
}

.mainNav li a:hover{
background: #ffffff;
}

.mainNav li ul.dropDown {
margin:0;
padding:0;
position: absolute;
display: none;
background: #fff;
opacity: 0.0;
}
.mainNav li ul.dropDown li {
float: left;
height: 28px;
line-height: 28px;
margin: 0;
padding: 0;
font-size: 12px;
color: #606060;
/*font-weight: normal;*/

}
.mainNav li ul.dropDown li a {
font-family: 'Raleway', sans-serif;
font-weight: 400;
line-height: 3em;
height: 28px;
display:block;
padding: 0 20px;
color: #606060;
}
.mainNav li ul.dropDown li a:hover {
background: #CFCFCF;
}

.img-logo-coach-menu {
position: absolute;
margin-left: 5em;
}

.active {
background: yellow;
}

最佳答案

将循环改为

var url = window.location.pathname;
var urlRegExp = new RegExp(url.replace(/\/$/,''));
$('.mainNav li a').each(function(){
// and test its href against the url pathname regexp
if(urlRegExp.test(this.href)){
$(this).addClass('active');
$(this).closest('.mainNav>li').children('a').addClass("active");
}
});

closest 函数找到与选择器匹配的第一个(最近)祖先,然后您可以将类添加到它的直接子级。

关于jquery - 为菜单 2 级添加事件,hiliting 2 级菜单 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30557937/

24 4 0