CSS子菜单未显示在主菜单下

时间:2014-06-30 21:45:47

标签: javascript html css menu

我有一个我正在研究的移动网站。我终于得到了JS代码,用于显示单击而不是悬停的子菜单。现在我无法直接在主菜单下定位子菜单。我研究了最好的,我发现我需要使主菜单位置“相对”和子菜单“绝对”。我一直在试图解决这个问题,但没有运气。代码有点乱,请原谅我。我只是个菜鸟。

HTML

<html>
<head>

<link rel="stylesheet" href="phone.css">
</head>
<body>

<img class="smlogo" alt="" src="clearlogo123.png">

<div id="menuclick" class="smenu_div">
    <ul>
            <li><a href="#">Menu</a>
</div>
<div id="hiddenMenu" class="smenu_div" style="display: none;">
                <ul>
                    <li><a href="#">1Submenu</a></li>
                    <li><a href="#">2Submenu</a></li>
                </ul>
</div>

            </li>


    </ul>   

<br>



<script>

var hidden = true;
document.getElementById('menuclick').onclick = function () {
    document.getElementById('hiddenMenu').style.display = (hidden) ? 'block' : 'none';
    hidden = !hidden;
};
</script>
</body></html>

CSS

.smenu_div ul
{
    padding:0px;
    margin-top:35px; 
    margin-right:40px;
    font-family:georgia;
    font-size:60px;
    color:#ffffff;
    list-style:none;
    text-indent:15px;
    text-align:center; 
    width:35%;
    float:right;
    overflow:hidden;
    position: relative;
    display: block;
}

.smenu_div ul li
{
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
    background:#000000;
    line-height:justified;
    border-bottom:1px solid #333;
    margin-top: 10px;
    z-index: 50;
    position:relative;
}
.smenu_div li ul
{ 
position:absolute;
float:right; 
}

.smenu_div ul li a
{
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
    text-decoration:none;
    color:#ffffff;
    display:block;
    position:relative;
}
.smenu_div ul li a:hover
{
    color:#000000;
    background:#fdff30;
}
.smenu_div ul li#active
{
    position:relative;
    color:#000000;
    background:#fdff30;
}

1 个答案:

答案 0 :(得分:0)

尝试更改HTML,以便在第一个li的中间没有div结尾。

然后,如果您将ID直接放在隐藏菜单上,您可以使用javascript直接隐藏/显示隐藏菜单

http://jsbin.com/fexazihe/1/edit?html,css,js,output

<强> HTML

<body>
<img class="smlogo" alt="" src="clearlogo123.png">

<div id="menuclick" class="smenu_div">
    <ul>
      <li>
        <a href="#">Menu</a>
        <ul id='hiddenMenu'>
           <li><a href="#">1Submenu</a></li>
           <li><a href="#">2Submenu</a></li>
         </ul>
        </li>
    </ul>   
</div>
</body>

<强> CSS

.smenu_div ul
{
    padding:0px;
    margin-top:35px; 
    margin-right:40px;
    font-family:georgia;
    font-size:60px;
    color:#ffffff;
    list-style:none;
    text-indent:15px;
    text-align:center; 
    width:35%;
    overflow:hidden;
    position: relative;
    display: block;
}

.smenu_div ul li
{
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
    background: rgba(0,0,0,0);
    line-height:justified;
    border-bottom:1px solid #333;
    margin-top: 10px;
    z-index: 50;
    position:relative;
}


/* Changed this so that your hidden menu is hidden by default */
.smenu_div li ul
{ 
  display: none;
  position: relative;
  width: 100%;
  background: transparent;
}

.smenu_div ul li a
{
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
    text-decoration:none;
    color:#ffffff;
  background: #000000;
    display:block;
    position:relative;
}
.smenu_div ul li a:hover
{
    color:#000000;
    background:#fdff30;
}
.smenu_div ul li#active
{
    position:relative;
    color:#000000;
    background:#fdff30;
}

<强> JS

<script>
    var hidden = true;
    document.getElementById('menuclick').onclick = function () {
        document.getElementById('hiddenMenu').style.display = (hidden) ? 'block' : 'none';
        hidden = !hidden;
    };
</script>