web菜单代码

原创 2006年06月18日 13:03:00

<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style>
.level_0{font-size:14px;background-color:#000080;color:#FFFFFF;}
.level_1{font-size:12px;background-color:#008080;color:#FFFFFF;}
.level_2{font-size:11px;background-color:#808080;color:#FFFFFF;}
</style>

<script>
// 定义菜单数据,格式:ID,父ID,层,文字,超级连接,目标框架
var menuData = [["div_0_1","",0,"菜单00","","_self"],
["div_1_1","div_0_1",1,"菜单11","","_self"],
["div_2_1","div_1_1",2,"菜单21","#","_self"],
["div_2_2","div_1_1",2,"菜单22","#","_self"],
["div_1_2","div_0_1",1,"菜单12","","_self"],
["div_2_3","div_1_2",2,"菜单23","#","_self"],
["div_2_4","div_1_2",2,"菜单24","#","_self"],
["div_1_3","div_0_1",1,"菜单13","#","_self"],
["div_1_4","div_0_1",1,"菜单14","#","_self"],
["div_0_2","",0,"菜单01","","_self"],
["div_1_5","div_0_2",1,"菜单15","#","_self"],
["div_1_6","div_0_2",1,"菜单16","#","_self"],
["div_1_7","div_0_2",1,"菜单17","#","_self"],
["div_1_8","div_0_2",1,"菜单18","#","_self"]];

// 定义菜单点击事件
function clickDiv(theDiv,level){
// 打开超级链接的标志
var urlFlag = false;
for(var i=0;i<menuData.length;i++){
// 如果有超级链接,打开超级链接
if(menuData[i][0]==theDiv.id && menuData[i][4].length>0){
// 打开网页
urlFlag = true;
//alert(menuData[i][4]);
location.href = menuData[i][4];
}
}
if(!urlFlag){
// 展开/折叠菜单
for(var i=0;i<menuData.length;i++){
if(menuData[i][1]==theDiv.id){
// 展开子菜单
if(document.all(menuData[i][0]).style.display == "none"){
document.all(menuData[i][0]).style.display = "block";
}else{
document.all(menuData[i][0]).style.display = "none";
}
}else{
// 折叠其他菜单
if (menuData[i][2]>level && menuData[i][2]>0){
document.all(menuData[i][0]).style.display = "none";
}
}
}
}
}

// 画菜单
function drawMenu(){
for(var i=0;i<menuData.length;i++){
switch (menuData[i][2]){
// 判断层次
case 0:
// 第0层
document.write("<div id=/""+menuData[i][0]+"/" onclick=/"clickDiv(this,"+menuData[i][2]+")/" class=/"level_0/" style=/"cursor:hand/">"+menuData[i][0]+"</div>");
break;
case 1:
// 第1层
document.write("<div id=/""+menuData[i][0]+"/" onclick=/"clickDiv(this,"+menuData[i][2]+")/" class=/"level_1/" style=/"display:none;cursor:hand;/">"+menuData[i][0]+"</div>");
break;
case 2:
//第2层
document.write("<div id=/""+menuData[i][0]+"/" onclick=/"clickDiv(this,"+menuData[i][2]+")/" class=/"level_2/" style=/"display:none;cursor:hand;/">"+menuData[i][0]+"</div>");
break;
default:
break;
}
}
}

</script>
</head>

<body>
<script>drawMenu();</script>
</body>

</html>

 

代码
<script language="JavaScript">
NS4 = (document.layers) ? 1 : 0;
IE4 = (document.all) ? 1 : 0;
ver4 = (NS4 || IE4) ? 1 : 0;

if (ver4) {
    with (document) {
        write("<STYLE TYPE='text/css'>");
        if (NS4) {
            write(".parent {position:absolute; visibility:visible}");
            write(".child {position:absolute; visibility:visible}");
            write(".regular {position:absolute; visibility:visible}")
        }
        else {
            write(".child {display:none}")
        }
        write("</STYLE>");
    }
}

function getIndex(el) {
    ind = null;
    for (i=0; i<document.layers.length; i++) {
        whichEl = document.layers[i];
        if (whichEl.id == el) {
            ind = i;
            break;
        }
    }
    return ind;
}

function arrange() {
    nextY = document.layers[firstInd].pageY +document.layers[firstInd].document.height;
    for (i=firstInd+1; i<document.layers.length; i++) {
        whichEl = document.layers[i];
        if (whichEl.visibility != "hide") {
            whichEl.pageY = nextY;
            nextY += whichEl.document.height;
        }
    }
}

function initIt(){
    if (!ver4) return;
    if (NS4) {
        for (i=0; i<document.layers.length; i++) {
            whichEl = document.layers[i];
            if (whichEl.id.indexOf("Child") != -1) whichEl.visibility = "hide";
       }
        arrange();
    }
    else {
        divColl = document.all.tags("DIV");
        for (i=0; i<divColl.length; i++) {
            whichEl = divColl(i);
            if (whichEl.className == "child") whichEl.style.display = "none";
        }
    }
}

function expandIt(el) {
    if (!ver4) return;
    if (IE4) {
        whichEl = eval(el + "Child");
        if (whichEl.style.display == "none") {
            whichEl.style.display = "block";
        }
        else {
            whichEl.style.display = "none";
        }
    }
    else {
        whichEl = eval("document." + el + "Child");
        if (whichEl.visibility == "hide") {
            whichEl.visibility = "show";
        }
        else {
            whichEl.visibility = "hide";
        }
        arrange();
    }
}
onload = initIt;
</script>
        </font></p>
      <div id="KB1Parent" class="parent">    <a href="#" onClick="expandIt('KB1'); return false" ><img src="img/plus.gif" border=0>文件夹一</a></div>
      <div id="KB1Child" class="child">     <a href="sample.htm" target="_blank" ><img src="img/spacer.gif"  border=0 alt=""><img src="img/open.gif"  border=0>页面一</a><br/>
             <a href="sample.htm" target="_blank" ><img src="img/spacer.gif"  border=0 alt="" width="15" height="11"><img src="img/open.gif"  border=0>页面二</a><br/>
             <a href="sample.htm" target="_blank" ><img src="img/spacer.gif"  border=0 alt="" width="15" height="11"><img src="img/open.gif"  border=0>页面三</a></div>
      <div id="KB2Parent" class="parent">    <a href="#" onClick="expandIt('KB2'); return false" ><img src="img/plus.gif" border=0>文件夹二</a></div>
      <div id="KB2Child" class="child">     <a href="sample.htm" target="_blank" ><img src="img/spacer.gif"  border=0 alt="" width="15" height="11"><img src="img/open.gif"  border=0>页面一</a><br/>
             <a href="sample.htm" target="_blank" ><img src="img/spacer.gif"  border=0 alt=""><img src="img/open.gif"  border=0>页面二</a><br/>
             <a href="sample.htm" target="_blank" ><img src="img/spacer.gif"  border=0 alt="" width="15" height="11"><img src="img/open.gif"  border=0>页面三</a></div>
      <div id="KB3Parent" class="parent">    <a href="#" onClick="expandIt('KB3'); return false" ><img src="img/plus.gif"  border=0>文件夹三</a></div>
      <div id="KB3Child" class="child">      <a href="sample.htm" target="_blank" ><img src="img/spacer.gif"  border=0 alt=""><img src="img/open.gif"  border=0>页面一</a><br/>
              <a href="sample.htm" target="_blank" ><img src="img/open.gif"  border=0>页面二</a><br/>
             <a href="sample.htm" target="_blank" ><img src="img/spacer.gif"  border=0 alt=""><img src="img/open.gif"  border=0>页面三</a></div>
      <script language="JavaScript">
if (NS4) {
        firstEl = "KB1Parent";
        firstInd = getIndex(firstEl);
        arrange();
}
</script>
着是三层的菜单,可以扩展成更多层次.


<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style>
.level_0{font-size:14px;background-color:#000080;color:#FFFFFF;}
.level_1{font-size:12px;background-color:#008080;color:#FFFFFF;}
.level_2{font-size:11px;background-color:#808080;color:#FFFFFF;}
</style>

<script>
// 定义菜单数据,格式:ID,父ID,层,文字,超级连接,目标框架
var menuData = [["div_0_1","",0,"菜单00","","_self"],
["div_1_1","div_0_1",1,"菜单11","","_self"],
["div_2_1","div_1_1",2,"菜单21","#","_self"],
["div_2_2","div_1_1",2,"菜单22","#","_self"],
["div_1_2","div_0_1",1,"菜单12","","_self"],
["div_2_3","div_1_2",2,"菜单23","#","_self"],
["div_2_4","div_1_2",2,"菜单24","#","_self"],
["div_1_3","div_0_1",1,"菜单13","#","_self"],
["div_1_4","div_0_1",1,"菜单14","#","_self"],
["div_0_2","",0,"菜单01","","_self"],
["div_1_5","div_0_2",1,"菜单15","#","_self"],
["div_1_6","div_0_2",1,"菜单16","#","_self"],
["div_1_7","div_0_2",1,"菜单17","#","_self"],
["div_1_8","div_0_2",1,"菜单18","#","_self"]];

// 定义菜单点击事件
function clickDiv(theDiv,level){
// 打开超级链接的标志
var urlFlag = false;
for(var i=0;i<menuData.length;i++){
// 如果有超级链接,打开超级链接
if(menuData[i][0]==theDiv.id && menuData[i][4].length>0){
// 打开网页
urlFlag = true;
//alert(menuData[i][4]);
location.href = menuData[i][4];
}
}
if(!urlFlag){
// 展开/折叠菜单
for(var i=0;i<menuData.length;i++){
if(menuData[i][1]==theDiv.id){
// 展开子菜单
if(document.all(menuData[i][0]).style.display == "none"){
document.all(menuData[i][0]).style.display = "block";
}else{
document.all(menuData[i][0]).style.display = "none";
}
}else{
// 折叠其他菜单
if (menuData[i][2]>level && menuData[i][2]>0){
document.all(menuData[i][0]).style.display = "none";
}
}
}
}
}

// 画菜单
function drawMenu(){
for(var i=0;i<menuData.length;i++){
switch (menuData[i][2]){
// 判断层次
case 0:
// 第0层
document.write("<div id=/""+menuData[i][0]+"/" onclick=/"clickDiv(this,"+menuData[i][2]+")/" class=/"level_0/" style=/"cursor:hand/">"+menuData[i][0]+"</div>");
break;
case 1:
// 第1层
document.write("<div id=/""+menuData[i][0]+"/" onclick=/"clickDiv(this,"+menuData[i][2]+")/" class=/"level_1/" style=/"display:none;cursor:hand;/">"+menuData[i][0]+"</div>");
break;
case 2:
//第2层
document.write("<div id=/""+menuData[i][0]+"/" onclick=/"clickDiv(this,"+menuData[i][2]+")/" class=/"level_2/" style=/"display:none;cursor:hand;/">"+menuData[i][0]+"</div>");
break;
default:
break;
}
}
}

</script>
</head>

<body>
<script>drawMenu();</script>
</body>

</html>

相关文章推荐

web菜单代码

  • 2013年05月26日 12:11
  • 3KB
  • 下载

Slideout.js – 滑出式 Web App 导航菜单

Slideout.js 是为您的移动 Web 应用开发的触摸滑出式的导航菜单。它没有依赖,自由搭配简单的标记,支持原生的滚动,您可以轻松地定制它。它支持不同的 CSS3 转换和过渡。最重要的是,它只是...

AS3仿web菜单

  • 2015年08月08日 18:09
  • 262KB
  • 下载

Web前端开发实战2:二级下拉式菜单之JS实现

上一篇博文提到了二级下拉式菜单是用HTML和CSS实现的,我们这一篇来用JavaScript脚本实现下拉菜单的显 示和隐藏。使用 JavaScript方法实现我们需要用的知识有:         1)...

【jQuery】smartMenu右键自定义上下文菜单插件(似web QQ)

一、这是什么样的一个插件 浏览器默认的右键选项有时候并不是我们所需要的,我们希望浏览器的右键选项菜单更智能,可以灵活自定义。比较有代表性的就是web QQ,例如下面截图: QQ邮箱中也...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:web菜单代码
举报原因:
原因补充:

(最多只允许输入30个字)