<script language="javascript" type="text/javascript">
</script>
效 果 演 示:
JavaScript代码:
var LastLeftID = "";
function menuFix(){
var obj = document.getElementById("nav").getElementsByTagName("li");
for (var i=0; i<obj.length; i++) {
obj[i].οnmοuseοver=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
obj[i].onMouseDown=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
obj[i].onMouseUp=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
obj[i].οnmοuseοut=function() {
this.className=this.className.replace(new RegExp("( ?|^)sfhover/b"), "");
}
}
}
function DoMenu(emid){
var obj = document.getElementById(emid);
obj.className = (obj.className.toLowerCase() == "expanded"?"collapsed":"expanded");
if((LastLeftID!="")&&(emid!=LastLeftID)) //关闭上一个Menu
{
document.getElementById(LastLeftID).className = "collapsed";
}
LastLeftID = emid;
}
function GetMenuID(){
var MenuID="";
var _paramStr = new String(window.location.href);
var _sharpPos = _paramStr.indexOf("#");
if (_sharpPos >= 0 && _sharpPos < _paramStr.length - 1){
_paramStr = _paramStr.substring(_sharpPos + 1, _paramStr.length);
}else{
_paramStr = "";
}
if (_paramStr.length > 0){
var _paramArr = _paramStr.split("&");
if (_paramArr.length>0){
var _paramKeyVal = _paramArr[0].split("=");
if (_paramKeyVal.length>0){
MenuID = _paramKeyVal[1];
}
}
}
if(MenuID!=""){
DoMenu(MenuID)
}
}
GetMenuID(); //*这两个function的顺序要注意一下,不然在Firefox里GetMenuID()不起效果
menuFix();
function menuFix(){
var obj = document.getElementById("nav").getElementsByTagName("li");
for (var i=0; i<obj.length; i++) {
obj[i].οnmοuseοver=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
obj[i].onMouseDown=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
obj[i].onMouseUp=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
obj[i].οnmοuseοut=function() {
this.className=this.className.replace(new RegExp("( ?|^)sfhover/b"), "");
}
}
}
function DoMenu(emid){
var obj = document.getElementById(emid);
obj.className = (obj.className.toLowerCase() == "expanded"?"collapsed":"expanded");
if((LastLeftID!="")&&(emid!=LastLeftID)) //关闭上一个Menu
{
document.getElementById(LastLeftID).className = "collapsed";
}
LastLeftID = emid;
}
function GetMenuID(){
var MenuID="";
var _paramStr = new String(window.location.href);
var _sharpPos = _paramStr.indexOf("#");
if (_sharpPos >= 0 && _sharpPos < _paramStr.length - 1){
_paramStr = _paramStr.substring(_sharpPos + 1, _paramStr.length);
}else{
_paramStr = "";
}
if (_paramStr.length > 0){
var _paramArr = _paramStr.split("&");
if (_paramArr.length>0){
var _paramKeyVal = _paramArr[0].split("=");
if (_paramKeyVal.length>0){
MenuID = _paramKeyVal[1];
}
}
}
if(MenuID!=""){
DoMenu(MenuID)
}
}
GetMenuID(); //*这两个function的顺序要注意一下,不然在Firefox里GetMenuID()不起效果
menuFix();
DIV+CSS代码:
*{margin:0;padding:0;border:0;}
#nav {
width:180px;
line-height: 24px;
list-style-type: none;
text-align:left;
/*定义整个ul菜单的行高*/
}
/*==================一级目录===================*/
#nav a {
width: 220px;
display: block;
padding-left:10px;
font-size:13px;
/*Width(一定要),否则下面的Li会变形*/
}
#nav li {
background:#D0FFFF; /*一级目录的背景色*/
border-bottom:#FFF 1px solid; /*下面的一条白边*/
float:left;
//float:left,本不应该设置,但由于在Firefox不能正常显示继承Nav的width,限制宽度,li自动向下延伸
}
#nav li a:hover{
background:#CC0000; /*一级目录onMouseOver显示的背景色*/
}
#nav a:link {
color:#666; text-decoration:none;
}
#nav a:visited {
color:#666;text-decoration:none;
}
#nav a:hover {
color:#FFF;text-decoration:none;font-weight:bold;
}
/*==================二级目录===================*/
#nav li ul {
list-style:none;
text-align:left;
}
#nav li ul li{
background:#ACD6FF; /*二级目录的背景色*/
}
#nav li ul a{
padding-left:30px;
width:200px;
/* padding-left二级目录中文字向右移动,但Width必须重新设置=(总宽度-padding-left)*/
}
/*下面是二级目录的链接样式*/
#nav li ul a:link {
color:#666; text-decoration:none;
}
#nav li ul a:visited {
color:#666;text-decoration:none;
}
#nav li ul a:hover {
color:#F3F3F3;
text-decoration:none;
font-weight:normal;
background:#CC0000;
/* 二级onmouseover的字体颜色、背景色*/
}
/*==============================*/
#nav li:hover ul {
left: auto;
}
#nav li.sfhover ul {
left: auto;
}
#content {
clear: left;
}
#nav ul.collapsed{
display: none;
}
#PARENT{
width:300px;
padding-left:20px;
}
#nav {
width:180px;
line-height: 24px;
list-style-type: none;
text-align:left;
/*定义整个ul菜单的行高*/
}
/*==================一级目录===================*/
#nav a {
width: 220px;
display: block;
padding-left:10px;
font-size:13px;
/*Width(一定要),否则下面的Li会变形*/
}
#nav li {
background:#D0FFFF; /*一级目录的背景色*/
border-bottom:#FFF 1px solid; /*下面的一条白边*/
float:left;
//float:left,本不应该设置,但由于在Firefox不能正常显示继承Nav的width,限制宽度,li自动向下延伸
}
#nav li a:hover{
background:#CC0000; /*一级目录onMouseOver显示的背景色*/
}
#nav a:link {
color:#666; text-decoration:none;
}
#nav a:visited {
color:#666;text-decoration:none;
}
#nav a:hover {
color:#FFF;text-decoration:none;font-weight:bold;
}
/*==================二级目录===================*/
#nav li ul {
list-style:none;
text-align:left;
}
#nav li ul li{
background:#ACD6FF; /*二级目录的背景色*/
}
#nav li ul a{
padding-left:30px;
width:200px;
/* padding-left二级目录中文字向右移动,但Width必须重新设置=(总宽度-padding-left)*/
}
/*下面是二级目录的链接样式*/
#nav li ul a:link {
color:#666; text-decoration:none;
}
#nav li ul a:visited {
color:#666;text-decoration:none;
}
#nav li ul a:hover {
color:#F3F3F3;
text-decoration:none;
font-weight:normal;
background:#CC0000;
/* 二级onmouseover的字体颜色、背景色*/
}
/*==============================*/
#nav li:hover ul {
left: auto;
}
#nav li.sfhover ul {
left: auto;
}
#content {
clear: left;
}
#nav ul.collapsed{
display: none;
}
#PARENT{
width:300px;
padding-left:20px;
}
<body>与</body>之间代码:
<div id="PARENT">
<ul id="nav">
<li>
<a href="#Menu=ChildMenu1" οnclick="DoMenu('ChildMenu1')">第一章 JavaScript显示日期时间</a>
<ul id="ChildMenu1" class="collapsed">
<li><a href="unit 1-1.html" target="_blank">日期星期</a></li>
<li><a href="unit 1-2.html" target="_blank">数字时钟</a></li>
<li><a href="unit 1-3.html" target="_blank">背景数字时钟</a></li>
</ul>
</li>
<li>
<a href="#Menu=ChildMenu2" οnclick="DoMenu('ChildMenu2')">第二章 JavaScript显示菜单效果</a>
<ul id="ChildMenu2" class="collapsed">
<li><a href="unit 2-1.html" target="_blank">导航条式菜单</a></li>
<li><a href="unit 2-2-1.html">二维树型菜单</a></li>
<li><a href="unit 2-2-2.html">二维下拉推动式菜单</a></li>
<li><a href="unit 2-2-3.html">二维隐藏下拉式菜单</a></li>
<li><a href="unit 2-2-4.html">二维隐藏右显示式菜单</a></li>
</ul>
</li>
<li><a href="#">以下章节还在建设中</a></li>
</ul>
</div>
<ul id="nav">
<li>
<a href="#Menu=ChildMenu1" οnclick="DoMenu('ChildMenu1')">第一章 JavaScript显示日期时间</a>
<ul id="ChildMenu1" class="collapsed">
<li><a href="unit 1-1.html" target="_blank">日期星期</a></li>
<li><a href="unit 1-2.html" target="_blank">数字时钟</a></li>
<li><a href="unit 1-3.html" target="_blank">背景数字时钟</a></li>
</ul>
</li>
<li>
<a href="#Menu=ChildMenu2" οnclick="DoMenu('ChildMenu2')">第二章 JavaScript显示菜单效果</a>
<ul id="ChildMenu2" class="collapsed">
<li><a href="unit 2-1.html" target="_blank">导航条式菜单</a></li>
<li><a href="unit 2-2-1.html">二维树型菜单</a></li>
<li><a href="unit 2-2-2.html">二维下拉推动式菜单</a></li>
<li><a href="unit 2-2-3.html">二维隐藏下拉式菜单</a></li>
<li><a href="unit 2-2-4.html">二维隐藏右显示式菜单</a></li>
</ul>
</li>
<li><a href="#">以下章节还在建设中</a></li>
</ul>
</div>