如何创建没有嵌套列表的下拉菜单?

时间:2014-12-18 21:53:33

标签: javascript jquery css drop-down-menu html-lists

我正在尝试实现一个基本的css下拉菜单,列表中没有嵌套列表。换句话说,正常的方式是:

<ul>
    <li><a>Blah</a></li>
    <li><a>Blah Parent</a>
        <ul>
            <li><a>Blah Child</a></li>
        </ul>
    </li>
</ul>

我需要这样做的方式是:

<ul>
    <li><a>Blah</a></li>
    <li><a>Blah Parent</a></li>
    <li class="childitem"><a>Blah Parent</a><li>
    <li class="childitem"><a>Blah Parent</a><li>
    <li><a>Blah</a></li>
    <li><a>Blah</a></li>
    <li class="childitem"><a>Blah Parent</a><li>
    <li class="childitem"><a>Blah Parent</a><li>
</ul>

有没有办法用纯粹的CSS进行此操作?或者有一些基本的js帮助?

4 个答案:

答案 0 :(得分:1)

确实,“有了一些基本的js帮助”,你只需要一个简单的“单线程”(在你的函数中)来修复HTML,这里它是格式化以提高可读性(以及评论中的解释):

function nestSubULbyClass(par, cls){
  for( var elms=par.getElementsByTagName('li'), L=elms.length, t
     ; L-- //loop through elements in reverse to avoid nested look-ahead loop(s)
     ; ~(' '+elms[L].className+' ').indexOf(cls)  //if   current elm contains specified class
       ? (t || (t=document.createElement('ul'))   //then take temp UL or create a new one
         ).appendChild(par.removeChild(elms[L]))  //     and move current elm to temp UL
       : t && (elms[L].appendChild(t), t=0)       //else append temp UL to current elm 
     );                                           //     and clear temp with falsy value
}

//"Make it so, number one!!"  Note this is just an example, hook and pass UL any way you like
window.onload=function(){
  nestSubULbyClass(document.getElementsByTagName('ul')[0], 'childitem');
};
<ul>
    <li><a>Blah</a></li>
    <li><a>Blah Parent</a></li>
    <li class="childitem"><a>Blah Parent</a></li>
    <li class="childitem"><a>Blah Parent</a></li>
    <li><a>Blah</a></li>
    <li><a>Blah</a></li>
    <li class="childitem"><a>Blah Parent</a></li>
    <li class="childitem"><a>Blah Parent</a></li>
</ul>

修改
从现在开始,人们现在可以简单地使用任何基于纯CSS的下拉菜单(和样式),而无需任何其他javascript处理程序(很好)!

对于以下示例,我使用了来自this answer的css。注意:我从单行中删除了注释和格式(幽默任何人都认为非上下文感知标准格式更适合阅读☺)

function nestSubULbyClass(par, cls) {
  for(var elms=par.getElementsByTagName('li'), L=elms.length, t; L--; ~(' '+elms[L].className+' ').indexOf(cls) ? (t || (t=document.createElement('ul'))).appendChild(par.removeChild(elms[L])) : t && (elms[L].appendChild(t), t=0));
}

window.onload=function(){
  nestSubULbyClass(document.getElementsByTagName('ul')[0], 'childitem');
};
ul {
    font-family: Arial, Verdana;
    font-size: 14px;
    margin: 0;
    padding: 0;
    list-style: none;
}

ul li {
    display: block;
    position: relative;
    float: left;
}

li ul {
    display: none;
}

ul li a {
    display: block;
    text-decoration: none;
    color: #ffffff;
    border-top: 1px solid #ffffff;
    padding: 5px 15px 5px 15px;
    background: #2C5463;
    margin-left: 1px;
    white-space: nowrap;
}

ul li a:hover {
    background: #617F8A;
}

li:hover ul {
    display: block;
    position: absolute;
}

li:hover li {
    float: none;
    font-size: 11px;
}

li:hover a {
    background: #617F8A;
}

li:hover li a:hover {
    background: #95A9B1;
}
<ul>
    <li><a>Blah</a></li>
    <li><a>Blah Parent</a></li>
    <li class="childitem"><a>Blah Parent</a></li>
    <li class="childitem"><a>Blah Parent</a></li>
    <li><a>Blah</a></li>
    <li><a>Blah</a></li>
    <li class="childitem"><a>Blah Parent</a></li>
    <li class="childitem"><a>Blah Parent</a></li>
</ul>

答案 1 :(得分:1)

您想要的结果实际上有点模棱两可,但有几种方法可以解决这个问题。

解决方案1 ​​ - 更改DOM

如果你想动态更改DOM,可以使用jQuery&#39; nextUntil()找到要移动的兄弟元素:

JSFiddle: http://jsfiddle.net/TrueBlueAussie/9afavum5/

// Solution 1 - change the DOM
$("ul li:not(.childitem)").each(function () {
    var children = $(this).nextUntil(":not(.childitem)");
    if (children.length) {
        $(this).append($("<ul>").append(children));
    }
});

这可以通过找到预期的父母(:not有.childitem),然后使用nextUntil找到兄弟姐妹直到下一个父元素。

Soution 2 - 切换现有

如果您只是想切换到子项,而不是更改DOM,则可以使用类似的匹配技术来查找兄弟项:

JSFiddle: http://jsfiddle.net/TrueBlueAussie/9afavum5/1/

// Solution 2 - toggle the siblings
$("ul li.childitem").hide();
$("ul li:not(.childitem)").click(function(){
    $(this).nextUntil(":not(.childitem)").toggle();
});

答案 2 :(得分:0)

你可以只有得到的遗嘱和遗嘱FIDDLE HERE

下面

<ul>
    <li><a>Blah</a></li>
    <li class="drp"><a>Blah Parent</a></li>
    <li class="childitem"><a>Blah Parent</a><li>
    <li class="childitem"><a>Blah Parent</a><li>
</ul>

JS

$(document).ready(function(){
  $(".drp").hover(function(){
       $(".childitem").slideToggle("fast");
    },function(){
         $(".childitem").hide();
  });
  $(".childitem").hover(function(){
       $(".childitem").show();
    },function(){
         $(".childitem").hide();
  });
});

CSS

.drp:hover
{
    cursor: pointer;   
}
ul li
{
    list-style-type: none;
}
.childitem
{
    display: none;
}
.childitem:hover
{
    background: green;
    cursor: pointer;
}

答案 3 :(得分:0)

Here is an example which uses CSS and a javascript.

<div class="topnav" id="myTopnav">
  <a href="home.html" class="active">Home</a>

  <div class="dropdown">
    <button class="dropbtn">Products
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="plinkone.html">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>

  <div class="dropdown">
    <button class="dropbtn">Services
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="slinkone.html">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>

 <a href="a.html">About</a>
  <a href="news.html">News</a>
  <a href="c.html">Contact</a>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
</div>

Here is the javascript:

<script>
function myFunction() {
    var x = document.getElementById("myTopnav");

    if (!x.classList.contains("responsive")) {
        x.classList.add("responsive");
    } else {

        x.classList.remove("responsive");
    }
}
</script>

and the CSS

.topnav {
  background-color: #333;
  overflow: ;

}

.topnav::after {
    clear: both;
    content: "";
    display: block;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.active {
  background-color: ;
  color: white;
}

.topnav .icon {
  display: none;
}

.dropdown {
    float: left;
    overflow: ;
}

.dropdown .dropbtn {
    font-size: 17px;   
    border: none;
    outline: none;
    color: white;
    padding: 14px 16px;
    background-color: #333;
    font-family: inherit;
    margin: 0;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content a {
    float: none;
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.topnav a:hover, .dropdown:hover .dropbtn {
  background-color: #555;
  color: white;
}

.dropdown-content a:hover {
    background-color: #ddd;
    color: black;
}

.dropdown:hover .dropdown-content {
    display: block;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child), .dropdown .dropbtn {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: relative;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}