关闭

web菜单代码

标签: webdivjavascripthtml框架ie
1639人阅读 评论(1) 收藏 举报
分类:

<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>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:59331次
    • 积分:1076
    • 等级:
    • 排名:千里之外
    • 原创:39篇
    • 转载:16篇
    • 译文:1篇
    • 评论:7条
    最新评论