多级(下拉)菜单css js

时间:2015-04-16 12:09:35

标签: javascript html css

我想要多级(下拉)菜单,这样我只需要将菜单更改为一个文件,而不必转到每个页面来更改菜单。我需要三级菜单

我修改了某些代码,但没有像我一样工作

CSS代码(OurSite.css)

ul#third-level-menu
{
  position: absolute;
  top: 0;
  right: -150px;
  width: 150px;
  list-style: none;
  padding: 0;
  margin: 0;
  display: none;
}

ul#third-level-menu > li
{
  height: 30px;
  background: #999999;
}

ul#third-level-menu > li:hover { background: #CCCCCC; }

ul#second-level-menu
{
  position: absolute;
  top: 30px;
  left: 0;
  width: 150px;
  list-style: none;
  padding: 0;
  margin: 0;
  display: none;
}

ul#second-level-menu > li
{
  position: relative;
  height: 30px;
  background: #999999;
}
ul#second-level-menu > li:hover { background: #CCCCCC; }

ul#top-level-menu
{
  list-style: none;
  padding: 0;
  margin: 0;
}

ul#top-level-menu > li
{
  position: relative;
  float: left;
  height: 30px;
  width: 150px;
  background: #999999;
}
ul#top-level-menu > li:hover { background: #CCCCCC; }

ul#top-level-menu li:hover > ul
{
/* On hover, display the next level's menu */
display: inline;
}


/* Menu Link Styles */

ul#top-level-menu a /* Apply to all links inside the multi-level menu */
{
  font: bold 14px Arial, Helvetica, sans-serif;
  color: #FFFFFF;
  text-decoration: none;
  padding: 0 0 0 10px;

/* Make the link cover the entire list item-container */
  display: block;
  line-height: 30px;
}

ul#top-level-menu a:hover { color: #000000; }

我也有 js代码(Menu-Script.js)

document.getElementById("nav01").innerHTML =
"<ul id='top-level-menu'>" +
"<li><a href='index.html'>Home</a></li>" +

"<ul id='second-level-menu'>" +
"<li><a href='index12.html'>Home12</a></li>" +


 "<ul id='third-level-menu'>" +
 "<li><a href='index123.html'>Home123</a></li>" +
 "<li><a href='index124.html'>Home124</a></li>" +
 "</ul>" +

 "<li><a href='index13.html'>Home13</a></li>" +

 "</ul>"  +

"<li><a href='customers.html'>Data</a></li>" +
"<li><a href='about.html'>About</a></li>" +
"</ul>"; 

对于HTML代码

<!DOCTYPE html>

<html>
 <head>
  <title>Demo</title>
  <link href="OurSite.css" rel="stylesheet">
 </head>
 <body>
  <nav id="nav01"></nav>
  <div id="main">
    <h1>Welcome to Our Site</h1>
    <h2>Web Site Main Ingredients:</h2>

    <p>Pages (HTML)</p>
    <p>Style (CSS)</p>
    <p>Code (JavaScript)</p>
  </div>
  <script src="Menu-Script.js"></script>
 </body>
</html>

或者,如果您有更好的代码请求,请告诉我,非常感谢

1 个答案:

答案 0 :(得分:0)

嗯......你的代码不是最好的,但是,我认为你只更改js里面的html:

document.getElementById("nav01").innerHTML =
"<ul id='top-level-menu'>" +
"<li><a href='index.html'>Home</a>" + // not close li here

"<ul id='second-level-menu'>" +
"<li><a href='index12.html'>Home12</a>" + // not close li here


 "<ul id='third-level-menu'>" +
 "<li><a href='index123.html'>Home123</a></li>" +
 "<li><a href='index124.html'>Home124</a></li>" +
 "</ul></li>" + // close li here

 "<li><a href='index13.html'>Home13</a></li>" +

 "</ul></li>"  + // close li here

"<li><a href='customers.html'>Data</a></li>" +
"<li><a href='about.html'>About</a></li>" +
"</ul>"; 

在其父<ul>内插入<li>

[ul] 
 -> li 
    [ul]
     -> li 
        [ul] 
         -> li
         -> li
 -> li
 -> li

示例:http://fiddle.jshell.net/9tok0z5d/