表格应用1
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
window.onload = function(){
var oTb = document.getElementById('tab1');
var oldColor = '';
for (var i =0;i<oTb.tBodies[0].rows.length;i++) {
oTb.tBodies[0].rows[i].onmouseover =function() {
oldColor = this.style.background;
this.style.background = 'green';
}
oTb.tBodies[0].rows[i].onmouseout =function() {
this.style.background = oldColor;
}
if(i%2)
{
oTb.tBodies[0].rows[i].style.background ='red';
}else
{
oTb.tBodies[0].rows[i].style.background ='';
}
}
//对表格进行操作
var oBtn = document.getElementById('btn1');
var oAge = document.getElementById('age');
var oName = document.getElementById('name');
var id = oTb.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);
//删除a标签对应的行
oTd.getElementsByTagName('a')[0].onclick = function(){
oTb.tBodies[0].removeChild(this.parentNode.parentNode);
}
//添加行
oTb.tBodies[0].appendChild(oTr);
}
}
</script>
</head>
<body>
姓名:<input id="name" type="text" />
年龄:<input id="age" type="text" />
<input id="btn1" type="button" value="添加" />
<table id='tab1' border="1" width="500" >
<thead>
<td>ID</td>
<td>姓名</td>
<td>年龄</td>
<td>操作</td>
</thead>
<tbody>
<tr>
<td>1</td>
<td>王五</td>
<td>23</td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>李四</td>
<td>22</td>
<td></td>
</tr>
<tr>
<td>3</td>
<td>张三</td>
<td>29</td>
<td></td>
</tr>
<tr>
<td>4</td>
<td>赫尔</td>
<td>34</td>
<td></td>
</tr>
<tr>
<td>5</td>
<td>航意</td>
<td>12</td>
<td></td>
</tr>
</tbody>
</table>
</body>
</html>
表格搜索
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
window.onload = function(){
var oTb = document.getElementById('tab1');
var oBtn = document.getElementById('btn1');
var oName = document.getElementById('name');
oBtn.onclick = function(){
for (var i=0;i<oTb.tBodies[0].rows.length;i++) {
//不区分大小写搜索
var sTab = oTb.tBodies[0].rows[i].cells[1].innerHTML.toLowerCase();
var sTxt = oName.value.toLowerCase();
//实现空格分段搜索
var arr = sTxt.split(' ');
oTb.tBodies[0].rows[i].style.background = '';
for (var j = 0; j < arr.length; j++) {
//实现模糊搜索
if(sTab.search(arr[j])!=-1){
//实现高亮显示
oTb.tBodies[0].rows[i].style.background = 'yellow';
}
}
}
}
}
</script>
</head>
<body>
姓名:<input id="name" type="text" />
<input id="btn1" type="button" value="搜索" />
<table id='tab1' border="1" width="500" >
<thead>
<td>ID</td>
<td>姓名</td>
<td>年龄</td>
<td>操作</td>
</thead>
<tbody>
<tr>
<td>1</td>
<td>王五</td>
<td>23</td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>李四</td>
<td>22</td>
<td></td>
</tr>
<tr>
<td>3</td>
<td>张三</td>
<td>29</td>
<td></td>
</tr>
<tr>
<td>4</td>
<td>Blue</td>
<td>34</td>
<td></td>
</tr>
<tr>
<td>5</td>
<td>航意</td>
<td>12</td>
<td></td>
</tr>
</tbody>
</table>
</body>
</html>