-
Notifications
You must be signed in to change notification settings - Fork 6
/
task8.html
184 lines (181 loc) · 5.48 KB
/
task8.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<!DOCTYPE html >
<html>
<head>
<meta charset="utf-8">
<title>任务八:JavaScript和树(二)</title>
<style type="text/css">
.box div {
display: flex;
justify-content: space-around;
align-items: center;
border: 1px solid #000;
background-color: #fff;
margin: 5px;
}
.level1 {
width: 1100px;
height: 200px;
}
.level2 {
height: 150px;
}
.level3 {
height: 100px;
}
.level4 {
height: 50px;
}
</style>
</head>
<body>
<input id="DFT" type="button" name="name" value="深度优先遍历" />
<input id="BFT" type="button" name="name" value="广度优先遍历" />
<input id="search-text" type="text" name="name" placeholder="请输入查询内容" />
<input id="DF-search" type="button" name="name" value="深度优先搜索" />
<input id="BF-search" type="button" name="name" value="广度优先搜索" />
<div class="box">
<div class="level1" id="root">super
<div class="level2">cat
<div class="level3">apple
<div class="level4">pear
</div>
<div class="level4">pig
</div>
<div class="level4">cup
</div>
</div>
<div class="level3">phone
</div>
<div class="level3">human
<div class="level4">book
</div>
<div class="level4">school
</div>
</div>
</div>
<div class="level2">note
<div class="level3">human
<div class="level4">code
</div>
<div class="level4">man
</div>
<div class="level4">pen
</div>
</div>
<div class="level3">program
<div class="level4">pointer
<div class="level5">function
</div>
</div>
<div class="level4">glass
</div>
</div>
</div>
<div class="level2">fish
</div>
</div>
</div>
<script type="text/javascript" src="EventUtil.js"></script>
<script type="text/javascript">
var nodeList = [];
var rootNode = document.getElementById("root");
var BFindex = 0;//广度优先遍历自增标识符
var found = false;
var foundNode = [];
/**
*traverseDF
*深度优先遍历
*/
function traverseDF(node) {
if (node) {
nodeList.push(node);
var child = node.children;
for (var i = 0, len = child.length; i < len; i++) {
traverseDF(child[i]);
}
}
}
/**
*traverseBF
*广度优先遍历
*/
function traverseBF(node) {
if (node) {
nodeList.push(node);
traverseBF(node.nextElementSibling);
node = nodeList[BFindex++];
traverseBF(node.firstElementChild);
}
}
/**
*show
*动画展示
*/
function show(searchText) {
var i = 0;
found = false;
var timer = setInterval(function () {
if (i < nodeList.length) {
if (i - 1 >= 0) {
nodeList[i - 1].style.backgroundColor = '#fff';
}
nodeList[i].style.backgroundColor = '#00f';
if (searchText && searchText == nodeList[i].firstChild.nodeValue.replace(/(^\s*)|(\s*$)/g, "")) {//获取元素本身的文本内容
nodeList[i].style.backgroundColor = '#f00';
foundNode.push(nodeList[i]);
found = true;
}
i++;
}
else {
clearInterval(timer);
nodeList[i - 1].style.backgroundColor = '#fff';
foundStyle('found');
if (searchText && !found) {
alert("未找到查询内容!");
}
}
}, 200);
}
function foundStyle (state) {
for (var i = 0, len = foundNode.length; i < len; i++) {
if (state == 'found') {
foundNode[i].style.backgroundColor = '#f00';
}
else if (state == 'reset') {
foundNode[i].style.backgroundColor = '#fff';
}
}
}
function DFTShow(searchText) {
nodeList = [];
foundStyle('reset');
foundNode = [];
traverseDF(rootNode);
show(searchText);
}
function BFTShow(searchText) {
BFindex = 0;
nodeList = [];
foundStyle('reset');
foundNode = [];
traverseBF(rootNode);
show(searchText);
}
EventUtil.addHandler(document.getElementById("DFT"), "click", function () {
DFTShow('');
});
EventUtil.addHandler(document.getElementById("BFT"), "click", function () {
BFTShow('');
});
EventUtil.addHandler(document.getElementById("DF-search"), "click", function () {
var searchText = document.getElementById("search-text").value;
DFTShow(searchText);
});
EventUtil.addHandler(document.getElementById("BF-search"), "click", function () {
var searchText = document.getElementById("search-text").value;
BFTShow(searchText);
});
</script>
</body>
</html>