表格應(yīng)用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 ='';
}
}
//對(duì)表格進(jìn)行操作
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標(biāo)簽對(duì)應(yīng)的行
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++) {
//不區(qū)分大小寫搜索
var sTab = oTb.tBodies[0].rows[i].cells[1].innerHTML.toLowerCase();
var sTxt = oName.value.toLowerCase();
//實(shí)現(xiàn)空格分段搜索
var arr = sTxt.split(' ');
oTb.tBodies[0].rows[i].style.background = '';
for (var j = 0; j < arr.length; j++) {
//實(shí)現(xiàn)模糊搜索
if(sTab.search(arr[j])!=-1){
//實(shí)現(xiàn)高亮顯示
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>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者