動態生成二級導航菜單

 根據得到的json數據動態生成二級導航菜單

jsp:頁面

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 
  2. <
  3. String path = request.getContextPath(); 
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  5. %> 
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  8. <html> 
  9.   <head> 
  10.     <base href="<%=basePath%>"> 
  11.      
  12.     <title>My JSP 'testMent.jsp' starting page</title> 
  13.      
  14.     <meta http-equiv="pragma" content="no-cache"> 
  15.     <meta http-equiv="cache-control" content="no-cache"> 
  16.     <meta http-equiv="expires" content="0">     
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
  18.     <meta http-equiv="description" content="This is my page"> 
  19.     <style> 
  20. *{ 
  21.     margin:0; 
  22.     padding:0; 
  23. .clear:after { 
  24.     clear: both; 
  25.     content: "."; 
  26.     display: block; 
  27.     height: 0; 
  28.     visibility: hidden; 
  29. nav{ 
  30.     display:inline-block; 
  31.     border:1px solid #505255; 
  32.     border-bottom: 1px solid #282C2F; 
  33.     -moz-border-radius: 5px; 
  34.     -webkit-border-radius: 5px; 
  35.     margin:50px; 
  36.     -webkit-box-shadow:1px 1px 3px #292929; 
  37.     -moz-box-shadow:1px 1px 3px #292929; 
  38. li{ 
  39.     list-style:none; 
  40.     float:left; 
  41.     border-right: 1px solid #2E3235; 
  42.     position: relative; 
  43.     /*background: -moz-linear-gradient(top, #fff, #555D5F 2% ,#555D5F  50%,#3E4245 100%); 
  44.     background: -webkit-gradient(linear, 0 0, 0 100%, from(#fff), color-stop(2%, #555D5F), color-stop(50%, #555D5F),to(#3E4245));*/ 
  45.     background:#555D5F; 
  46. li:hover{ 
  47.     /*background: -moz-linear-gradient(top, #fff, #3E4245 2% ,#555D5F  80%,#555D5F 100%); 
  48.     background: -webkit-gradient(linear, 0 0, 0 100%, from(#fff), color-stop(2%, #3E4245), color-stop(80%, #3E4245),to(#555D5F));*/ 
  49.     background:#3E4245; 
  50.     -moz-transition: background 1s ease-out; 
  51.     -webkit-transition: background 1s ease-out; 
  52. li a{ 
  53.     display:block; 
  54.     height:40px; 
  55.     line-height:40px; 
  56.     padding:0 30px; 
  57.     font-size:12px; 
  58.     color:#fff; 
  59.     text-shadow: 0px -1px 0px #000; 
  60.     text-decoration:none; 
  61.     white-space:nowrap; 
  62.     border-left: 1px solid #999E9F; 
  63.     border-top: 1px solid #999E9F; 
  64.     -moz-border-top-left-radius: 2px; 
  65.     -webkit-border-top-left-radius: 2px; 
  66.      
  67.     z-index:100; 
  68. li > a{ 
  69.     position:relative; 
  70. li.first a{ 
  71.     -moz-border-radius-topleft: 4px; 
  72.     -moz-border-radius-bottomleft: 4px; 
  73.     -webkit-border-top-left-radius: 4px; 
  74.     -webkit-border-bottom-left-radius: 4px; 
  75. li.last{ 
  76.     border-right: 0 none; 
  77.  
  78.  
  79. dl{ 
  80.     position:absolute; 
  81.     display:block; 
  82.     top:40px; 
  83.     left: -25px; 
  84.      
  85.     width:165px; 
  86.      
  87.     background:#222222; 
  88.     -moz-border-radius: 5px; 
  89.     -webkit-border-radius: 5px; 
  90.      
  91.     -webkit-box-shadow:1px 1px 3px #292929; 
  92.     -moz-box-shadow:1px 1px 3px #292929; 
  93.      
  94.     z-index:10; 
  95.              
  96. li:hover dl{ 
  97.     top:50px; 
  98.     display:block; 
  99.     width:145px; 
  100.     padding:10px; 
  101. dl a{ 
  102.     background:transparent; 
  103.     border:0 none; 
  104.     -moz-border-radius: 5px; 
  105.     -webkit-border-radius: 5px; 
  106.     -moz-transition: background 0.5s ease-out; 
  107.     -webkit-transition: background 0.5s ease-out; 
  108.      
  109.     z-index:50; 
  110. dl a:hover{ 
  111.     color:#FFF; 
  112.     background:#999E9F; 
  113.     -moz-transition: background 0.3s ease-in-out, color 0.3s ease-in-out; 
  114.     -webkit-transition: background 0.3s ease-in-out, color 0.3s ease-in-out; 
  115. dd{ 
  116.     margin-top:-40px; 
  117.     opacity:0; 
  118.     width:145px; 
  119.     -webkit-transition-property:all; 
  120.     /*-webkit-transition-timing-function: cubic-bezier(5,0,5,0);*/ 
  121.     -moz-transition-property: all; 
  122.     /*-moz-transition-timing-function: cubic-bezier(5,0,5,0);*/ 
  123.     /*-webkit-transition-delay:5s; 
  124.     -moz-transition-delay:5s;*/ 
  125. li:hover dd{ 
  126.     margin-top:0; 
  127.     opacity:1; 
  128. li dd:nth-child(1){ 
  129.     -webkit-transition-duration: 0.1s; 
  130.     -moz-transition-duration: 0.1s; 
  131. li dd:nth-child(2){ 
  132.     -webkit-transition-duration: 0.2s; 
  133.     -moz-transition-duration: 0.2s; 
  134. li dd:nth-child(3){ 
  135.     -webkit-transition-duration: 0.3s; 
  136.     -moz-transition-duration: 0.3s; 
  137. li dd:nth-child(4){ 
  138.     -webkit-transition-duration: 0.4s; 
  139.     -moz-transition-duration: 0.4s; 
  140. dt{ 
  141.     display:none; 
  142.     margin-top:-25px; 
  143.     padding-top:15px; 
  144.     height:10px; 
  145. li:hover dt{ 
  146.     display:block; 
  147. .Darrow{ 
  148.     float:right; 
  149.     margin:18px 10px 0 0; 
  150.     border-width:5px; 
  151.     border-color:#FFF transparent transparent transparent; 
  152.     border-style:solid; 
  153.     width:0; 
  154.     height:0; 
  155.     line-height:0; 
  156.     overflow:hidden; 
  157.      
  158.     cursor:pointer; 
  159.      
  160.     text-shadow: 0px -1px 0px #000; 
  161.      
  162.     -webkit-box-shadow:0px -1px 0px #000; 
  163.     -moz-box-shadow:0px -1px 0px #000; 
  164. .arrow{ 
  165.     margin:0 auto; 
  166.     margin-top:-5px; 
  167.     display:block; 
  168.     width:10px; 
  169.     height:10px; 
  170.     background:#222222; 
  171.     -webkit-transform: rotate(45deg); 
  172.     -moz-transform: rotate(45deg); 
  173. </style> 
  174.     <script type="text/javascript" src="javascript/jquery.min.js"></script> 
  175.     <script type="text/javascript"> 
  176.     function test(){ 
  177.      var s=''
  178.        $.ajax({ 
  179.         type:'get',//請求方式 
  180.         url:'json.json', // AJAX HTTP請求接口 'json.json'
  181.         data:'', 
  182.         dataType:'json',//請求類型爲json 
  183.         timeout:7000,//請求超時後停止請求 
  184.         success: function(json){ 
  185.          var d=json
  186.            
  187.            $.each(d,function(i){ 
  188.             
  189.               if(typeof d[i].children != "undefined" && d[i].children.length >0) 
  190.                 { 
  191.                 s+= '<li><span class="Darrow"></span><a href="#">'+d[i].text+'</a><dl><dt><span class="arrow"></span></dt>'; 
  192.                 $.each(d[i].children,function(j){ 
  193.                 s+=' <dd><a href="#">'+d[i].children[j].text+'</a></dd>'; 
  194.                 }); 
  195.                 s+='</dl></li>'; 
  196.                  
  197.                 } 
  198.                 else 
  199.                 { 
  200.                     s+='<li class="first"><a href="#">'+d[i].text+'</a></li>'; 
  201.                 } 
  202.                  
  203.             });           
  204.              $('#out').html(s); 
  205.     } 
  206.     }); 
  207.  
  208.     } 
  209.      
  210.     </script> 
  211.  
  212.   </head> 
  213.    
  214.   <body> 
  215.     <input type="button" onclick="test()" value="點擊生成菜單"> 
  216.    
  217.  <nav> 
  218.     <ul class="clear" id="out"> 
  219.          
  220.     </ul> 
  221. </nav> 
  222.   </body> 
  223. </html> 

json數據頁面:

 

  1. {"id":"90","text":"menu1","children":[{"id":"9010","text":"chidren1","children":[],"leaf":true,"iconCls":"menu-11","url":"jsp/sysManger/role.jsp"},{"id":"9020","text":"chidren2","children":[],"leaf":true,"iconCls":"menu-12","url":"jsp/sysManger/user.jsp"},{"id":"9030","text":"chidren3","children":[],"leaf":true,"iconCls":"menu-13","url":"jsp/sysManger/data.jsp"}],"leaf":false,"iconCls":"menu-1","url":null} 
  2. ,{"id":"90","text":"menu2","leaf":false,"iconCls":"menu-1","url":null} 
  3. ,{"id":"90","text":"menu3","children":[{"id":"9010","text":"chidren1","children":[],"leaf":true,"iconCls":"menu-11","url":"jsp/sysManger/role.jsp"},{"id":"9020","text":"chidren2","children":[],"leaf":true,"iconCls":"menu-12","url":"jsp/sysManger/user.jsp"},{"id":"9030","text":"chidren3","children":[],"leaf":true,"iconCls":"menu-13","url":"jsp/sysManger/data.jsp"}],"leaf":false,"iconCls":"menu-1","url":null} 
  4. ,{"id":"90","text":"menu4","children":[{"id":"9010","text":"chidren1","children":[],"leaf":true,"iconCls":"menu-11","url":"jsp/sysManger/role.jsp"},{"id":"9020","text":"chidren2","children":[],"leaf":true,"iconCls":"menu-12","url":"jsp/sysManger/user.jsp"},{"id":"9030","text":"chidren3","children":[],"leaf":true,"iconCls":"menu-13","url":"jsp/sysManger/data.jsp"}],"leaf":false,"iconCls":"menu-1","url":null} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章