需求:

运用AJAX请求文件menu.json,配置菜单栏,并实现以下功能点:

1. 点击向左箭头,菜单向左移动,隐藏
2. 点击向右箭头,菜单向右移动,显示
3. 点击一级菜单,被点击菜单的子菜单显示,其他兄弟节点的子菜单隐藏

页面显示:ajaxTest\WebRoot\nav.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ajax json jquery 菜单案例</title>
<style type="text/css">
*{margin:0;padding:0;}
body { font-size: 13px; line-height: 130%; padding: 60px }
</style>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script src="js/jquery-2.1.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$.ajax({
url: "data/menu.json",
success: function( data) {
var html = "<ul>";
for(var i=0; i<data.length; i++) {
html += "<li>" + data[i].topMenu +"<ul>";
var subData = data[i].subMenu;
for(var j=0; j<subData.length; j++) {
html += "<li>" +subData[j] + "</li>";
}
html += "</ul></li>";
}
html += "</ul>";
$(".nav-container").prepend(html);
$(".nav-container>ul>li").siblings().children().hide();
},
error:function(data){
alert(data);
}
}) $(".drag-handle").on("click", function() {
if($(this).hasClass("right")) {
$(this).text(">");
$(this).removeClass("right");
$(this).addClass("left");
$(this).parent().animate({"left": "-150px"}, 'slow');
} else {
$(this).text("<");
$(this).removeClass("left");
$(this).addClass("right");
$(this).parent().animate({"left": "0px"}, 'slow');
}
})
$(document).on("click", ".nav-container>ul>li", function() {
$(this).children().toggle();
$(this).siblings().children().hide();
return false;
})
})
</script>
</head>
<body>
<div class="nav-container">
<div class="drag-handle right"><</div>
</div>
</body>
</html>

菜单Json数据:ajaxTest\WebRoot\data\menu.json

 [
{
"topMenu": "菜单1",
"subMenu": [
"子菜单1",
"子菜单2",
"子菜单3"
]
},
{
"topMenu": "菜单2",
"subMenu": [
"子菜单1",
"子菜单2",
"子菜单3"
]
},
{
"topMenu": "菜单3",
"subMenu": [
"子菜单1",
"子菜单2",
"子菜单3"
]
},
{
"topMenu": "菜单4",
"subMenu": [
"子菜单1",
"子菜单2",
"子菜单3"
]
},
{
"topMenu": "菜单5",
"subMenu": [
"子菜单1",
"子菜单2",
"子菜单3"
]
},
{
"topMenu": "菜单6",
"subMenu": [
"子菜单1",
"子菜单2",
"子菜单3"
]
}
]

页面样式:ajaxTest\WebRoot\css\style.css

 .nav-container {
position: absolute;
top:;
left:;
width: 170px; }
ul {
width: 150px;
float: left;
background: #204d62;
}
ul li {
list-style: none;
line-height: 40px;
text-align: center;
color: #fff;
cursor: pointer;
}
ul li > ul {
background: #eee;
}
ul li > ul li {
color: #204d62;
}
.drag-handle {
float: left;
width:20px;
height:40px;
background: #204d62;
color: #fff;
font-weight: bold;
line-height: 40px;
text-align: center;
cursor: pointer;
}

最新文章

  1. java注解处理
  2. IO流 FileOutputSteam在fos.txt写出hello
  3. lua高阶用法 OO的实现
  4. Hierarchyid 常用操作
  5. Spark SQL External Data Sources JDBC官方实现读测试
  6. Basic knowledge of html (keep for myself)
  7. 实现一个基于FTP协议的程序——文件上传下载器(十三)
  8. NagiosQL 跨站脚本漏洞
  9. 45种Javascript技巧大全【转藏】
  10. Git命令详解(一)-个人使用
  11. 从 Kubernetes 谈容器网络
  12. SolrCloud的介绍
  13. 集合 (set) 的增删改查及 copy()方法
  14. eclipse发布web
  15. su: 无法设置用户ID: 资源暂时不可用
  16. ios开发之--多个按钮单选效果
  17. .NET Core中使用Dapper操作Oracle存储过程最佳实践
  18. Leetcode:Add Two Numbers分析和实现
  19. 构建微软智能云:介绍新的Azure业务转型创新技术
  20. Innodb和Mysiam引擎的区别

热门文章

  1. 关于View的ScrollTo, getScrollX 和 getScrollY
  2. 第20章 Redis配置
  3. php 获取随机字符串(原创)
  4. IPv6通讯原理(1) - 不能忽略的网卡启动过程
  5. LeetCode Weekly Contest 21
  6. j2ee学习笔记
  7. 快速登录MySQL数据库
  8. 使用postman模拟登录请求
  9. Windows 10 常用软件推荐
  10. Java学习笔记2——数据类型和转换