-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.表格操作.html
187 lines (163 loc) · 4.79 KB
/
15.表格操作.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
185
186
187
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
span{
font-size: 10px;
font-style: italic;
}
</style>
<script type="text/javascript">
window.onload=function(){
var oTab=document.getElementById("tab1");
var oldColor="";
function style(){
for (var i=0;i<oTab.tBodies[0].rows.length;i++) {
if (i%2) {
oTab.tBodies[0].rows[i].style.background="";
} else{
oTab.tBodies[0].rows[i].style.background="#ccc"
}
oTab.tBodies[0].rows[i].onmouseover=function(){
oldColor=this.style.background;
this.style.background="yellow";
}
oTab.tBodies[0].rows[i].onmouseout=function(){
this.style.background=oldColor;
}
}
}
style();
var oBtn=document.getElementById("add");
var oName=document.getElementById("name");
var oAge=document.getElementById("age");
var id=oTab.tBodies[0].rows.length+1;
oBtn.onclick=function(){
var oTr=document.createElement("tr");
var oTd=document.createElement("td");
oTd.innerHTML=id++;
oTr.appendChild(oTd);
var oTd=document.createElement("td");
oTd.innerHTML=oName.value;
oTr.appendChild(oTd);
var oTd=document.createElement("td");
oTd.innerHTML=oAge.value;
oTr.appendChild(oTd);
var oTd=document.createElement("td");
oTd.innerHTML="<a href='javascript:;'>删除</a>";
oTr.appendChild(oTd);
oTd.getElementsByTagName("a")[0].onclick=function(){
oTab.tBodies[0].removeChild(this.parentNode.parentNode);
}
oTab.tBodies[0].appendChild(oTr);
style();
}
var oBtn2=document.getElementById("btn2");
var oTxt2=document.getElementsByTagName("input")[3];
oBtn2.onclick=function(){
style();
for(var i=0;i<oTab.tBodies[0].rows.length;i++){
var sTab=oTab.tBodies[0].rows[i].cells[1].innerHTML.toLowerCase();//不区分大小写
var sTxt=oTxt2.value.toLowerCase();
var arr=sTxt.split(" ");//多关键词搜索
// oldColor=oTab.tBodies[0].rows[i].style.background;//刚开始用这个解决颜色问题,后来发现加上style()就没用了
//此处搜索结果高亮显示,如果想筛选出来,只需要设置高亮的地方改为display:block,其余为none
for (var j=0;j<arr.length;j++) {
if(sTab.search(arr[j])!=-1){ //search模糊搜索
oTab.tBodies[0].rows[i].style.background="red";
}
}
// if(sTab.search(sTxt)!=-1){ //search模糊搜索
// oTab.tBodies[0].rows[i].style.background="red";
// }
// else{
// oTab.tBodies[0].rows[i].style.background=oldColor;
// }
}
}
var oBtn3=document.getElementById("btn3");
oBtn3.onclick=function(){
var arr=[];
for(var i=0;i<oTab.tBodies[0].rows.length;i++){
arr[i]=oTab.tBodies[0].rows[i];//oTab看着像数组,但实际并不是,所以不能直接排序
}
arr.sort(function(tr1,tr2){
var n1=parseInt(tr1.cells[0].innerHTML);
var n2=parseInt(tr2.cells[0].innerHTML);
return n1-n2;
});
for(var i=0;i<arr.length;i++){
oTab.tBodies[0].appendChild(arr[i]);
}
style();
}
}
</script>
</head>
<body>
姓名:<input type="text" name="name" id="name" value="" />
年龄: <input type="text" name="age" id="age" value="" />
<input type="button" name="add" id="add" value="添加" />
<br /> <br />
姓名:<input type="text" name="" id="" value="" />
<input type="button" name="btn2" id="btn2" value="搜索" />
<span>注:不区分大小写、支持模糊搜索、多关键词搜索</span>
<br /> <br />
<input type="button" name="" id="btn3" value="按Id排序" />
<br /> <br />
<table border="1" width="450px" id="tab1">
<thead>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Operate</th>
</thead>
<tbody>
<tr>
<td>7</td>
<td>Stephen Hawking</td>
<td>76</td>
<td></td>
</tr>
<tr>
<td>1</td>
<td>张三</td>
<td>25</td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>李四</td>
<td>26</td>
<td></td>
</tr>
<tr>
<td>6</td>
<td>June</td>
<td>36</td>
<td></td>
</tr>
<tr>
<td>3</td>
<td>王五</td>
<td>39</td>
<td></td>
</tr>
<tr>
<td>4</td>
<td>赵柳</td>
<td>35</td>
<td></td>
</tr>
<tr>
<td>5</td>
<td>朱飞</td>
<td>27</td>
<td></td>
</tr>
</tbody>
</table>
</body>
</html>