使用Jquery添加其他下拉菜单

时间:2013-11-25 06:14:25

标签: jquery drop-down-menu

我正在尝试使用我的下拉菜单创建一个点击事件,但我似乎只能让第一个菜单下拉,一旦我点击菜单,我就无法让它消失。任何帮助,将不胜感激。我觉得它很简单。这是我的代码:

 <body>
    <div class="container">
        <ul id="nav">
            <li><a href="#">Home</a></li>
            <li id="clickMe"><a href="#s1">Menu 1</a>
                <span id="s1"></span>
                <ul class="subs">
                    <li><a href="#">Header a</a>
                        <ul>
                            <li><a href="#">Submenu x</a></li>
                            <li><a href="#">Submenu y</a></li>
                            <li><a href="#">Submenu z</a></li>
                        </ul>
                    </li>
                    <li><a href="#">Header b</a>
                        <ul>
                            <li><a href="#">Submenu x</a></li>
                            <li><a href="#">Submenu y</a></li>
                            <li><a href="#">Submenu z</a></li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li id="clickMe" ><a href="#s2">Menu 2</a>
                <span id="s2"></span>
                <ul class="subs">
                    <li ><a href="#">Header c</a>
                        <ul>
                            <li><a href="#">Submenu x</a></li>
                            <li><a href="#">Submenu y</a></li>
                            <li><a href="#">Submenu z</a></li>
                        </ul>
                    </li>
                    <li><a href="#">Header d</a>
                        <ul>
                            <li><a href="#">Submenu x</a></li>
                            <li><a href="#">Submenu y</a></li>
                            <li><a href="#">Submenu z</a></li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li ><a href="#s3">Menu 3</a>
                <span id="s3"></span>
                <ul class="subs">
                    <li><a href="#">Header e</a>
                        <ul>
                            <li><a href="#">Subsdfa</a></li>
                            <li><a href="#">Submenu y</a></li>
                            <li><a href="#">Submenu z</a></li>
                        </ul>
                    </li>
                    <li><a href="#">Header f</a>
                        <ul>
                            <li><a href="#">Submenu x</a></li>
                            <li><a href="#">Submenu y</a></li>
                            <li><a href="#">Submenu z</a></li>
                        </ul>
                    </li>

                    <li><a href="#">Header g</a>
                        <ul>
                            <li><a href="#">Submenu x</a></li>
                            <li><a href="#">Submenu y</a></li>
                            <li><a href="#">Submenu z</a></li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li><a href="#">Menu 4</a></li>
            <li><a href="#">Menu 5</a></li>
            </ul>
    </div>

    <script type="text/javascript">
    $('#clickMe').click(function(){
    $('#nav li ul.subs').toggle();

    });
    </script>



   </body>

这是CSS

  * {
margin: 0;
padding: 0;
}
html {
background-color: #fff;
height: 100%;
 }
body {
color: #333333;
font: 0.75em/1.5em Arial,sans-serif;
 }
.container {
margin-left: auto;
margin-right: auto;
margin-top: 30px;
width: 96%;
}

  /* common and top level styles */
#nav span {
display: none;
}
 #nav, #nav ul {
list-style: none outside none;
margin: 0;
padding: 0;
}
#nav {
background-color: #F5F5F5;
border-bottom: 5px solid #333333;
float: left;
margin-left: 1%;
margin-right: 1%;
position: relative;
width: 98%;
}
#nav ul.subs {
background-color: #FFFFFF;
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
color: #333333;
display: none;
left: 0;
padding: 2%;
position: absolute;
top: 54px;
width: 96%;
}
 #nav > li {
border-bottom: 5px solid transparent;
float: left;
margin-bottom: -5px;
text-align: left;
-moz-transition: all 300ms ease-in-out 0s;
-ms-transition: all 300ms ease-in-out 0s;
-o-transition: all 300ms ease-in-out 0s;
-webkit-transition: all 300ms ease-in-out 0s;
transition: all 300ms ease-in-out 0s;
}
#nav li a {
display: block;
text-decoration: none;
-moz-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
-ms-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
-o-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
-webkit-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
white-space: normal;
}
#nav > li > a {
color: #333333;
display: block;
font-size: 1.3em;
line-height: 49px;
padding: 0 15px;
text-transform: uppercase;
}
#nav > li:hover > a, #nav > a:hover {
background-color: #F55856;
color: #FFFFFF;
 }
#nav li.active > a {
background-color: #333333;
color: #FFFFFF;
 }

 /* submenu */
 #nav li:hover ul.subs {
 display: none;
 }


 #nav ul.subs > li {
 display: inline-block;
 float: none;
 padding: 10px 2%;
 vertical-align: top;
 width: 8%;
  }
 #nav ul.subs > li a {
 color: #777777;
 line-height: 20px;
 }
 #nav ul li a:hover {
 color: #F55856;
 }
 #nav ul.subs > li > a {
  font-size: 1.3em;
  margin-bottom: 10px;
  text-transform: uppercase;
  }
 #nav ul.subs > li li {
 float: none;
 padding-left: 8px;
 -moz-transition: padding 150ms ease-out 0s;
 -ms-transition: padding 150ms ease-out 0s;
 -o-transition: padding 150ms ease-out 0s;
 -webkit-transition: padding 150ms ease-out 0s;
 transition: padding 150ms ease-out 0s;
  }
 #nav ul.subs > li li:hover {
  padding-left: 15px;
  }

感谢您的任何建议。我只是希望导航在我点击它时停下来并留在那里直到我点击或转到下一个菜单。

1 个答案:

答案 0 :(得分:0)

需要删除所有重复ID。根据定义,ID必须是唯一的。改为上课

你可以试试这个:

 $('#nav > li').click(function () {
      $(this).siblings().find('ul.subs').hide();
      $(this).find('ul.subs').toggle();
  });

点击完成打开/关闭。但是,使用hover

可能会更加用户友好

DEMO

类似但使用hover

 $('#nav > li').hover(function () {
      $(this).siblings().find('ul.subs').hide();
      $(this).find('ul.subs').show();
  },function(){
       $(this).find('ul.subs').show();
  });

DEMO

注意:悬停版本可以在没有使用CSS的脚本的情况下完成