jsp页面

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.     pageEncoding="UTF-8"%> 
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  4. <html> 
  5. <head> 
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  7. <link type="text/css" rel="stylesheet" href="css/menu.css"></link> 
  8. <script type="text/javascript" src="js/jquery-1.4.2.js"></script> 
  9. <script type="text/javascript" src="js/menu.js"></script> 
  10. <title>横向纵向菜单</title> 
  11. </head> 
  12. <body> 
  13.     <ul> 
  14.             <li class="main"> 
  15.                     <a href="#">菜单项1</a> 
  16.                 <ul> 
  17.                     <li> 
  18.                         <a href="#">子菜单项11</a> 
  19.                     </li> 
  20.                     <li> 
  21.                         <a href="#">子菜单项12</a> 
  22.                     </li> 
  23.                 </ul> 
  24.             </li> 
  25.             <li class="main"> 
  26.                 <a href="#">菜单项2</a> 
  27.                 <ul> 
  28.                     <li> 
  29.                         <a href="#">子菜单项21</a> 
  30.                     </li> 
  31.                     <li> 
  32.                         <a href="#">子菜单项22</a> 
  33.                     </li> 
  34.                 </ul> 
  35.             </li> 
  36.             <li class="main"> 
  37.                 <a href="#">菜单项3</a> 
  38.                 <ul> 
  39.                     <li> 
  40.                         <a href="#">子菜单项31</a> 
  41.                     </li> 
  42.                     <li> 
  43.                         <a href="#">子菜单项32</a> 
  44.                     </li> 
  45.                 </ul> 
  46.             </li> 
  47.         </ul> 
  48.         <br /> 
  49.         <br /> 
  50.         <br /> 
  51.         <ul> 
  52.             <li class="hmain"> 
  53.                     <a href="#">菜单项1</a> 
  54.                 <ul> 
  55.                     <li> 
  56.                         <a href="#">子菜单项11</a> 
  57.                     </li> 
  58.                     <li> 
  59.                         <a href="#">子菜单项12</a> 
  60.                     </li> 
  61.                 </ul> 
  62.             </li> 
  63.             <li class="hmain"> 
  64.                 <a href="#">菜单项2</a> 
  65.                 <ul> 
  66.                     <li> 
  67.                         <a href="#">子菜单项21</a> 
  68.                     </li> 
  69.                     <li> 
  70.                         <a href="#">子菜单项22</a> 
  71.                     </li> 
  72.                 </ul> 
  73.             </li> 
  74.             <li class="hmain"> 
  75.                 <a href="#">菜单项3</a> 
  76.                 <ul> 
  77.                     <li> 
  78.                         <a href="#">子菜单项31</a> 
  79.                     </li> 
  80.                     <li> 
  81.                         <a href="#">子菜单项32</a> 
  82.                     </li> 
  83.                 </ul> 
  84.             </li> 
  85.         </ul> 
  86. </body> 
  87. </html> 

css样式

  1. ul,li{  
  2.     /*清除ul和li上默认的小圆点*/ 
  3.     list-stylenone;  
  4. }  
  5.  
  6. ul {  
  7.     /*清除子菜单的缩进值*/ 
  8.     padding0;  
  9.     margin0;  
  10. }  
  11. .main,.hmain {  
  12.     background-imageurl(../images/title.gif);  
  13.     background-repeatrepeat-x;  
  14.     width120px;  
  15. }  
  16. li {  
  17.     background-color#EEEEEE;  
  18. }  
  19. a {  
  20.     /*取消所有的下划线*/ 
  21.     text-decorationnone;  
  22.     padding-left20px;  
  23.     displayblock;  
  24.     display: inline-block;  
  25.     width100px;  
  26.     padding-top3px;  
  27.     padding-bottom3px;  
  28. }  
  29. .main a, .hmain a {  
  30.     colorwhite;  
  31.     background-imageurl(../images/collapsed.gif);  
  32.     background-repeatno-repeat;  
  33.     background-position3px center;  
  34. }  
  35. .main li a, .hmain li a {  
  36.     colorblack;  
  37.     background-imagenone;  
  38. }  
  39. .main ul, .hmain ul {  
  40.     displaynone;  
  41. }  
  42. .hmain {  
  43.     floatleft;  
  44.     margin-right1px;  

Jquery代码

 

  1. $(document).ready(function(){  
  2.     //页面中的DOM已经装载完成时,执行的代码  
  3.     $(".main > a").click(function(){  
  4.         //找到主菜单项对应的子菜单项  
  5.         var ulNode = $(this).next("ul");  
  6.         /*  
  7.         if (ulNode.css("display") == "none") {  
  8.             ulNode.css("display","block");  
  9.         } else {  
  10.             ulNode.css("display","none");  
  11.         }  
  12.         */ 
  13.         //ulNode.show("slow");//normal fast  
  14.         //ulNode.hide();  
  15.         //ulNode.toggle();  
  16.         //  
  17.         //ulNode.slideDown("slow");  
  18.         //ulNode.slideUp;  
  19.         ulNode.slideToggle();  
  20.         changeIcon($(this));  
  21.     });  
  22.     $(".hmain").hover(function(){  
  23.         $(this).children("ul").slideDown();  
  24.         changeIcon($(this).children("a"));  
  25.     },function(){  
  26.         $(this).children("ul").slideUp();  
  27.         changeIcon($(this).children("a"));  
  28.     });  
  29. });  
  30.  
  31. /**  
  32.  * 修改主菜单的指示图标  
  33.  */ 
  34. function changeIcon(mainNode) {  
  35.     if (mainNode) {  
  36.         if (mainNode.css("background-image").indexOf("collapsed.gif") >= 0) {  
  37.             mainNode.css("background-image","url('images/expanded.gif')");  
  38.         } else {  
  39.             mainNode.css("background-image","url('images/collapsed.gif')");  
  40.         }  
  41.     }  
  42. }