仅在使用CSS的子菜单中选择子项时突出显示父项?

时间:2015-03-10 07:48:22

标签: html css navigation

我一直在绞尽脑汁,直接进入谷歌搜索结果的第3页,但无济于事。这是html和css,如何在选择子菜单时使父(主菜单项)保持高亮显示。  非常感谢任何帮助!

<nav class="HeaderNav">
        <ul>
            <li><a href="#firstPage/1">Welcome</a></li>
            <li><a href="#firstPage/2">Megatrends</a></li>
            <li>
                <a href="#">Letters to shareholders</a>
                <ul class="test">
                    <li><a href="#firstPage/3">From Rodney O’Neal</a></li>
                    <li><a href="#firstPage/4">From Kevin P. Clark</a></li>
                </ul>
            </li>
            <li><a href="#firstPage/5">Financial highlights</a></li>
            <li><a href="#firstPage/6">At a glance</a></li>
            <li>
                <a href="#">Spotlights</a>
                <ul>
                    <li><a href="#firstPage/7">Innovation</a></li>
                    <li><a href="#firstPage/8">Collaboration</a></li>
                    <li><a href="#firstPage/9">Excellence</a></li>
                </ul>
            </li>
            <li>
                <a href="#">Our commitments</a>
                <ul>
                    <li><a href="#firstPage/10">Social responsibility</a></li>
                    <li><a href="#firstPage/11">Leadership</a></li>
                </ul>

和css:

<style>
nav ul li a:link { font-family:Arial, Helvetica, sans-serif; font-size:13px; color:#d7d7d7; text-decoration:none; padding:12px 12px 8px;}
nav ul li a:visited { font-family:Arial, Helvetica, sans-serif; font-size:13px; color:#d7d7d7; text-decoration:none; background-color:#000000; padding:12px 12px 8px;}
nav ul li a:hover { font-family:Arial, Helvetica, sans-serif; font-size:13px; color:#00aeef; text-decoration:none;  background-color:#000000; padding:12px 12px 8px;}
nav ul li a:active { font-family:Arial, Helvetica, sans-serif; font-size:13px; color:#00aeef; text-decoration:none; background-color:#000000; padding:12px 12px 8px;}
nav ul li a:focus { font-family:Arial, Helvetica, sans-serif; font-size:13px; color:#00aeef; text-decoration:none; background-color:#000000; padding:12px 12px 8px;}

nav ul ul {
    display:none;
    background: #000000; border-radius: 0px; padding: 0;
    position: absolute; top: 100%;
    text-align:left;
}

nav ul ul li {
    float: none; 
    border-bottom: 1px solid #939393;
    position: relative;
    padding:5px 0;
}

nav ul ul li a {
    padding: 5px 20px;
}   

nav ul ul li a:hover { font-family:Arial, Helvetica, sans-serif; font-size:13px; color:#00aeef; text-decoration:none; background-color:#000000; padding:5px 12px;}

nav ul li:hover > ul {
    display: block;
    }
</style>

1 个答案:

答案 0 :(得分:0)

需要jQuery,你必须通过给它们类来指定你的父元素和子元素。

然后,您可以在样式表中添加类.active并设置突出显示的样式。

&#13;
&#13;
$('.child').click(function () {
    $('li.active').removeClass('active');
    $(this).closest('.parent').addClass('active');
});
&#13;
nav ul li a:link {
    font-family:Arial, Helvetica, sans-serif;
    font-size:13px;
    color:#d7d7d7;
    text-decoration:none;
    padding:12px 12px 8px;
}
nav ul li a:visited {
    font-family:Arial, Helvetica, sans-serif;
    font-size:13px;
    color:#d7d7d7;
    text-decoration:none;
    background-color:#000000;
    padding:12px 12px 8px;
}
nav ul li a:hover {
    font-family:Arial, Helvetica, sans-serif;
    font-size:13px;
    color:#00aeef;
    text-decoration:none;
    background-color:#000000;
    padding:12px 12px 8px;
}
nav ul li a:active {
    font-family:Arial, Helvetica, sans-serif;
    font-size:13px;
    color:#00aeef;
    text-decoration:none;
    background-color:#000000;
    padding:12px 12px 8px;
}
nav ul li a:focus {
    font-family:Arial, Helvetica, sans-serif;
    font-size:13px;
    color:#00aeef;
    text-decoration:none;
    background-color:#000000;
    padding:12px 12px 8px;
}
nav ul li {
    display: inline-block;
    width: 100px;
}
nav ul ul {
    display:block;
    border-radius: 0px;
    padding: 0;
    position: absolute;
    top: 100px;
    text-align:left;
}
nav ul ul li {
    float: none;
    border-bottom: 1px solid #939393;
    position: relative;
    padding:5px 0;
    display: block;
}
nav ul ul li a {
    padding: 5px 20px;
}
nav ul ul li a:hover {
    font-family:Arial, Helvetica, sans-serif;
    font-size:13px;
    color:#00aeef;
    text-decoration:none;
    background-color:#000000;
    padding:5px 12px;
}
nav ul li:hover > ul {
    display: block;
}
.active {
    background: red;
    color: white;
}
&#13;
<nav class="HeaderNav">
    <ul>
        <li><a href="#firstPage/1">Welcome</a>

        </li>
        <li><a href="#firstPage/2">Megatrends</a>

        </li>
        <li class="parent"> <a href="#">Letters to shareholders</a>

            <ul class="test">
                <li class="child"><a href="#firstPage/3">From Rodney O’Neal</a>

                </li>
                <li class="child"><a href="#firstPage/4">From Kevin P. Clark</a>

                </li>
            </ul>
        </li>
        <li><a href="#firstPage/5">Financial highlights</a>

        </li>
        <li><a href="#firstPage/6">At a glance</a>

        </li>
        <li class="parent"> <a href="#">Spotlights</a>

            <ul>
                <li class="child"><a href="#firstPage/7">Innovation</a>

                </li>
                <li class="child"><a href="#firstPage/8">Collaboration</a>

                </li>
                <li class="child"><a href="#firstPage/9">Excellence</a>

                </li>
            </ul>
        </li>
        <li> <a href="#">Our commitments</a>

            <ul>
                <li><a href="#firstPage/10">Social responsibility</a>

                </li>
                <li><a href="#firstPage/11">Leadership</a>

                </li>
            </ul>
&#13;
&#13;
&#13;