2

大家好,当访问子菜单时,我如何才能始终li以背景突出显示父级“”,或者在下拉导航菜单上提供具有 css 某些属性的类?类似的东西将指示它正在导航的菜单的哪个部分,并且该背景始终保持突出显示,直到将更改菜单。

HTML

<ul id="menu">
    <li><a href="#">About </a></li>
    <li><a href="#">Programmes</a>
        <ul class="secondLevel">
            <li><a onClick="ajaxCall();" href="">Undergraduate</a></li>
            <li><a href="#">Postgraduate</a></li>
        </ul>
    </li>
    <li><a href="#">Academics</a></li>
    <li><a href="#">Research</a></li>
    <li><a href="#">Facilities</a></li>
</ul>

CSS

#menu, #menu ul {
    margin: 0;
    padding: 0;
    list-style: none;
} #menu {
    width: 958px;
    /*margin: 60px auto;*/
    border: 1px solid #222;
    background-color: #111;
    background-image: linear-gradient(#444, #111);

box-shadow:
    0 1px 1px #777;
} #menu:before, #menu:after {
    content: "";
    display: table;
} #menu:after {
    clear: both;
} #menu {
    zoom: 1;
} #menu li {
    float: left;
    border-right: 1px solid #222;
    box-shadow: 1px 0 0 #444;
    position: relative;
} #menu a {
    float: left;
    padding: 11px 40px;
    color: #999;
    text-transform: uppercase;
    text-decoration: none;
    text-shadow: 0 1px 0 #000;
} #menu li:hover > a {
    color: #fafafa;
} * html #menu li a:hover { /* IE6 only */
    color: #fafafa;
} #menu ul {
    margin: 20px 0 0 0;
    _margin: 0; /*IE6 only*/
    opacity: 0;
    visibility: hidden;
    position: absolute;
    top: 38px;
    left: 0;
    right: -1px;
    z-index: 1000000;
    background: #444;
    background: linear-gradient(#444, #111);

box-shadow:
    0 -1px 0 rgba(255, 255, 255, .3);

transition:all
    .2s

ease-in-out;
} #menu li:hover > ul {
    opacity: 1;
    visibility: visible;
    margin: 0;
} #menu ul ul {
    top: 0;
    left: 150px;
    margin: 0 0 0 20px;
    _margin: 0; /*IE6 only*/
    box-shadow: -1px 0 0 rgba(255, 255, 255, .3);
} #menu ul li {
    float: none;
    display: block;
    border: 0;
    _line-height: 0; /*IE6 only*/
    box-shadow: 0 1px 0 #111, 0 2px 0 #666;
} #menu ul li:last-child {
    box-shadow: none;
} #menu ul a {
    padding: 10px;
    _height: 10px; /*IE6 only*/
    display: block;
    white-space: nowrap;
    float: none;
    text-transform: none;
} #menu ul a:hover {
    background-color: #000000;
} #menu ul li:first-child > a {
    /*border-radius: 3px 3px 0 0;*/
    padding: 11px 13px;
} #menu ul li:first-child > a:after {
    content: '';
    position: absolute;
    left: 40px;
    top: -6px;
} #menu ul ul li:first-child a:after {
    left: -6px;
    top: 50%;
    margin-top: -6px;
    border-left: 0;
} #menu ul li:first-child a:hover:after {
    border-bottom-color: #000;
} #menu ul ul li:first-child a:hover:after {
    border-right-color: #000;
    border-bottom-color: transparent;
} #menu ul li:last-child > a {
    /*border-radius: 0 0 3px 3px*/;
    padding: 11px 20px;
}

jsFiddle

4

2 回答 2

7

您可以使用 jQuery 在.secondLevel元素上添加一个单击事件来做到这一点:

$(document).ready(function(){
    $('.secondLevel').find('li').click(function(){

       //removing the previous selected menu state
       $('#menu').find('li.active').removeClass('active');

       //adding the state for this parent menu
       $(this).parents('li').addClass('active');
    });
});

active并为菜单创建状态:

#menu li.active{
    background: #ccddff;
}

活生生的例子:http: //jsfiddle.net/L94N6/2/

更新

为了使它适用于两个列表,您可以根据列表类添加一个简单的条件:

$('#menu').find('li').click(function(){
   //removing the previous selected menu state
   $('#menu').find('li.active').removeClass('active');

    //is this element from the second level menu?
    if($(this).closest('ul').hasClass('secondLevel')){
         $(this).parents('li').addClass('active');

    //this is a parent element
    }else{
         $(this).addClass('active');
    }
});

活生生的例子:http: //jsfiddle.net/L94N6/4/

于 2013-07-03T13:48:26.893 回答
-1

如果我正确理解您的要求,您只需添加

#menu li:hover {
    background: red;
}

这是因为当内容处于悬停状态时,下拉父级的 li 的悬停状态仍然存在。

于 2013-07-03T13:47:32.903 回答