JavaScript+CSS实现的可折叠二级菜单实例
本文实例讲述了JavaScript+CSS实现的可折叠二级菜单。分享给大家供大家参考,具体如下:
.aspx文件:
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="NavigateMenu.aspx.cs"Inherits="NavigateMenu"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <headrunat="server"> <title>无标题页</title> <scripttype="text/javascript"src="JScript.js"></script> <styletype="text/css"> *{ margin:0px; padding:0px; border:0px; } #nav{ width:200px; margin:0px; padding:0px; font-size:14px; line-height:30px; } #navli{ width:180px; padding-left:20px; padding-bottom:1px; list-style-image:none; list-style-type:none; background-color:#FFFFFF; float:left; } #nava{ padding-left:20px; background-color:#BFBFBF; display:block; text-decoration:none; } #nava:hover{ background-color:#FF9175; } #navliul{ padding-top:1px; list-style-image:none; list-style-type:none; } #navlili{ padding-left:0px; } #navula{ background-color:#EBEBEB; } .cx{ display:none; visibility:hidden; } .ex{ display:inherit; visibility:inherit; } </style> <scripttype="text/javascript"> window.onload=function() { statUp(); } </script> </head> <body> <formid="form1"runat="server"> <divid="Parent"> <ulid="nav"> <li><ahref="javascript:void(0);"onclick="doMenu(this)">菜单1</a> <ul> <li><ahref="javascript:void(0);">菜单1_1</a></li> <li><ahref="javascript:void(0);">菜单1_2</a></li> </ul> </li> <li><ahref="javascript:void(0);"onclick="doMenu(this)">菜单2</a> <ul> <li><ahref="javascript:void(0);">菜单2_1</a></li> <li><ahref="javascript:void(0);">菜单2_2</a></li> </ul> </li> <li><ahref="javascript:void(0);"onclick="doMenu(this)">菜单3</a> <ul> <li><ahref="javascript:void(0);">菜单3_1</a></li> <li><ahref="javascript:void(0);">菜单3_2</a></li> </ul> </li> </ul> </div> </form> </body> </html>
js文件:
functiondoMenu(obj){ varitems=obj.parentNode.getElementsByTagName("ul"); varitmUl; if(items.length>0){ itmUl=items[0]; } if(itmUl.className!="ex"){ cxAll(); itmUl.className="ex"; }else{ itmUl.className="cx"; } } functionstatUp(){ cxAll(); } functioncxAll(){ varulDom=document.getElementById("nav"); varitems=ulDom.getElementsByTagName("ul"); for(vari=0;i<items.length;i++) { items[i].className="cx"; } }
在这里需要注意的是延迟加载的问题,由于页面加载时需要执行预处理操作statUp()方法,js单独写成一个文件或js写在<head>节点中时,需要使用window.onload=function(){执行语句;}延迟加载,不然引用DOM中对象时,会出现缺少对象错误提示。
别一种解决方法,直接将所有javaScript写在</html>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/> <title>菜单</title> <scripttype="text/javascript"src="Untitled-3.js"></script> <style> *{ margin:0px; padding:0px; border:0px; } #nav{ width:200px; margin:0px; padding:0px; font-size:14px; line-height:30px; } #navli{ width:180px; padding-left:20px; padding-bottom:1px; list-style-image:none; list-style-type:none; background-color:#FFFFFF; float:left; } #nava{ padding-left:20px; background-color:#BFBFBF; display:block; text-decoration:none; } #nava:hover{ background-color:#FF9175; } #navliul{ padding-top:1px; list-style-image:none; list-style-type:none; } #navlili{ padding-left:0px; } #navula{ background-color:#EBEBEB; } .cx{ display:none; visibility:hidden; } .ex{ display:inherit; visibility:inherit; } </style> </head> <body> <divid="Parent"> <ulid="nav"> <li><ahref="javascript:void(0);"onclick="doMenu(this)">菜单1</a> <ul> <li><ahref="javascript:void(0);">菜单1_1</a></li> <li><ahref="javascript:void(0);">菜单1_2</a></li> </ul> </li> <li><ahref="javascript:void(0);"onclick="doMenu(this)">菜单2</a> <ul> <li><ahref="javascript:void(0);">菜单2_1</a></li> <li><ahref="javascript:void(0);">菜单2_2</a></li> </ul> </li> <li><ahref="javascript:void(0);"onclick="doMenu(this)">菜单3</a> <ul> <li><ahref="javascript:void(0);">菜单3_1</a></li> <li><ahref="javascript:void(0);">菜单3_2</a></li> </ul> </li> </ul> </div> </body> </html> <scripttype="text/javascript"> functiondoMenu(obj){ varitems=obj.parentNode.getElementsByTagName("ul"); varitmUl; if(items.length>0){ itmUl=items[0]; } if(itmUl.className!="ex"){ cxAll(); itmUl.className="ex"; }else{ itmUl.className="cx"; } } functionstatUp(){ cxAll(); //varulDom=document.getElementById("nav"); //varitems=ulDom.getElementsByTagName("ul"); } functioncxAll(){ varulDom=document.getElementById("nav"); varitems=ulDom.getElementsByTagName("ul"); for(vari=0;i<items.length;i++) { items[i].className="cx"; } } statUp(); </script>
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。