forked from li-kang/frantendNav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontendNav.html
98 lines (78 loc) · 2.02 KB
/
frontendNav.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
<script>
$(function() {
var $content = $("#content");
var data;
showContent();
showAddNav();
function showContent() {
// 加载本地缓存的数据
if(!localStorage.nav2) {
// 如果没有缓存,加载服务器数据。
$.getJSON("jqnav.json", function(json) {
data = json;
show();
});
} else {
data = JSON.parse(localStorage.nav2);
show();
}
}
function createBlock(json, block) {
// div的样子
var $block = $("<div><p></p><ul></ul></div>");
// title
$block.find("p").html(block);
// items
var $ul = $block.find("ul");
for(var i = 0; i < json[block].length; i++) {
var item = json[block][i];
var $li = $("<li><span></span><p></p></li>");
$li.find("span").html(item.title);
$li.find("p").html(item.desc);
$li.wrapInner("<a href='" + item.url + "'></a>");
$ul.append($li);
}
// append
$content.append($block);
}
function showAddNav() {
var $add = $("#add");
$add.find("#btnAdd").click(function() {
var input = $add.find("input[type=text]");
var item = {};
var category = input.eq(0).val();
item.title = input.eq(1).val();
item.ulr = input.eq(2).val();
item.desc = input.eq(3).val();
data[category].push(item);
localStorage.nav2 = JSON.stringify(data);
show();
});
}
function show() {
$content.html("");
for(var block in data) {
createBlock(data, block);
}
}
});
</script>
</head>
<body>
<div id="add">
类别: <input type="text" value="Javascript" /> 网站:
<input type="text" /> 网址:
<input type="text" /> 推荐理由:
<input type="text" />
<input id="btnAdd" type="button" value="添加" />
</div>
<div id="content">
</div>
</body>
</html>