如何在使用Css的垂直侧边栏菜单中单击li时显示子菜单?

时间:2018-02-10 18:25:01

标签: html css

这里我有侧边栏菜单,这里我试图点击国家显示国家/地区名称。但问题是,只要我将鼠标悬停在国家上,就会显示国家/地区名称,而不是点击国家/地区显示国家/地区名称。

下面是我的HTML和CSS:



<nav id="nav">
    <ul class="mainmenu">
        <li><a href="#top" id="top-link" class="skel-layers-ignoreHref"><span class="icon fa-home">Intro</span></a>
        </li>
        <li>
            <a><span class="icon fa-envelope">Country</span></a>
            <ul class="submenu">
                <li><a href="">USA</a></li>
                <li><a href="">Australia</a></li>
                <li><a href="">UK</a></li>
            </ul>
        </li>
    </ul>
</nav>

<style>
    .mainmenu, .submenu {
        list-style: none;
        padding: 0;
        margin: 0;
    }

        .mainmenu a {
            display: block;
            text-decoration: none;
            padding: 10px;
            color: #000;
        }

        .mainmenu li:hover .submenu {
            display: block;
            max-height: 200px;
        }

    .submenu {
        overflow: hidden;
        max-height: 0;
        -webkit-transition: all 0.5s ease-out;
    }
</style>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

使用jquery进行onclick事件并删除hover css

var flag = 1;

    $('.icon').click(function() {
      if(flag == 1){
      $('.submenu').css('display', 'block');
      flag = 0;}
      else{
      $('.submenu').css('display', 'none');
      flag = 1;
      }
      
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <nav id="nav">
    <h5>click on the country</h5>
                    <ul class="mainmenu">
                        <li><a href="#top" id="top-link" class="skel-layers-ignoreHref"><span class="icon fa-home">Intro</span></a></li>              
                        <li>
                            <a><span cursor="help" class="icon fa-envelope">Country</span></a>
                            <ul class="submenu">
                                <li><a href="">USA</a></li>
                                <li><a href="">Australia</a></li>
                                <li><a href="">UK</a></li>
                            </ul>
                        </li>
                    </ul>
                </nav>
    <br><br>


         <style>
            .mainmenu, .submenu {
                list-style: none;
                padding: 0;
                margin: 0;
            }

                .mainmenu a {
                    display: block;
                    text-decoration: none;
                    padding: 10px;
                    color: #000;
                }

                .mainmenu li .submenu {
                    display: none;
                    max-height: 200px;
                }

            .submenu {
                overflow: hidden;
                max-height: 0;
                -webkit-transition: all 0.5s ease-out;
            }
        </style>