<!DOCTYPE html>
<html>
<head>
<title>From & Add & Remove & Search</title>
</head>
<script type="text/javascript">
window.onload=function()
{
var oTab=document.getElementById('tab1');
var oName=document.getElementById('name');
var oAge=document.getElementById('age');
var oDone=document.getElementById('done');
var aA=document.getElementsByTagName('a');
var oSearchTxt=document.getElementById('searchText')
var oSearchDone=document.getElementById('searchDone');
var oResetSearch=document.getElementById('resetSearch');
var oLength=oTab.tBodies[0].rows.length+1;
var oldColor='';
var oScreenText=document.getElementById('screenText');
var oScreenDone=document.getElementById('screenDone');
var oResetDone=document.getElementById('resetDone');
var oSort=document.getElementById("sort");
//排序
oSort.onclick=function(){
var arr=[];
for (var i = 0; i < oTab.tBodies[0].rows.length; i++) {
arr[i]=oTab.tBodies[0].rows[i];
}
arr.sort(function(a,b){
var a=parseInt(a.cells[0].innerHTML);
var b=parseInt(b.cells[0].innerHTML);
return a-b;
});
for (var i = 0; i < oTab.tBodies[0].rows.length; i++) {
oTab.tBodies[0].appendChild(arr[i]);
}
refresh();
}
//筛选
oScreenDone.onclick=function(){
for (var j = 0; j < oTab.tBodies[0].rows.length; j++) {
oTab.tBodies[0].rows[j].style.display='table-row';
}
for (var j = 0; j < oTab.tBodies[0].rows.length; j++) {
var sName=oTab.tBodies[0].rows[j].cells[1].innerHTML.toLowerCase();
var sText=oScreenText.value.toLowerCase();
if (sName.search(sText)==-1) {
refresh();
oTab.tBodies[0].rows[j].style.display='none';
}
}
}
//筛选重置
oResetDone.onclick=function(){
for (var j = 0; j < oTab.tBodies[0].rows.length; j++) {
oTab.tBodies[0].rows[j].style.display='table-row';
}
}
//测试
// alert(oTab.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[0].getElementsByTagName('td')[1].innerHTML);
// alert(oTab.tBodies[0].rows[0].cells[1].innerHTML)
// oDone.onclick=function(){
// oTab.tBodies[0].innerHTML+='<tr><td>'+ (oTab.tBodies[0].rows.length+1) +'</td><td>'+oName.value+'</td><td>'+oAge.value+'</td></tr>';
// }
// alert(aA.length);
//搜索
oSearchDone.onclick=function(){
if (oSearchTxt.value=='') {
refresh();
}
else{
for (var i = 0; i < oTab.tBodies[0].rows.length; i++) {
var oldColor2=oTab.tBodies[0].rows[i].style.background;
var sName=oTab.tBodies[0].rows[i].cells[1].innerHTML.toLowerCase();
var sText=oSearchTxt.value.toLowerCase();
//多个关键词--跟多行填充暂时有冲突
function moreTage(){
oTab.tBodies[0].rows[i].style.background='';
for (var j = 0; j < sText.length; j++) {
if (sName.search(sText[j])!=-1) {
oTab.tBodies[0].rows[i].style.background='yellow';
}
}
}
if (sName.search(sText)!==-1) {
refresh();
oTab.tBodies[0].rows[i].style.background='yellow';
}
else{
oTab.tBodies[0].rows[i].style.background=oldColor2;
}
}
}
}
//搜索重置
oResetSearch.onclick=function(){
refresh();
}
//删除
for (var i = 0; i < aA.length; i++) {
aA[i].onclick=function(){
oTab.tBodies[0].removeChild(this.parentNode.parentNode);
refresh();
}
}
//添加内容
oDone.onclick=function(){
oTr=document.createElement('tr');
oTd=document.createElement('td');
oTd.innerHTML=oLength++;
oTr.appendChild(oTd);
oTd=document.createElement('td');
oTd.innerHTML=oName.value;
oTr.appendChild(oTd);
oTd=document.createElement('td');
oTd.innerHTML=oAge.value;
oTr.appendChild(oTd);
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);
refresh();
}
//通过颜色区分每一行
function refresh(){
for (var i = 0; i < oTab.tBodies[0].rows.length; i++) {
oTab.tBodies[0].rows[i].onmouseover=function(){
oldColor=this.style.background;
this.style.background='#ddd';
}
oTab.tBodies[0].rows[i].onmouseout=function(){
this.style.background=oldColor;
}
if (i%2)
{
oTab.tBodies[0].rows[i].style.background='#eee';
}
else
{
oTab.tBodies[0].rows[i].style.background='';
}
}
}
refresh();
}
</script>
<body>
<input type="button" value="sort" id="sort">
<input type="text" id="searchText">
<input type="button" value="Search" id="searchDone">
<input type="button" value="Reset" id="resetSearch">
<br>
<input type="text" id="screenText">
<input type="button" value="Screen" id="screenDone">
<input type="button" value="Reset" id="resetDone">
<table id="tab1" border="1px" width="500px">
<thead>
<td>id</td>
<td> 姓名</td>
<td> 年龄</td>
<td> 操作</td>
</thead>
<tbody>
<tr>
<td>2</td>
<td>布鲁斯李</td>
<td>27</td>
<td><a href="javascript:;">删除</a></td>
</tr>
<tr>
<td>4</td>
<td>张三</td>
<td>17</td>
<td><a href="javascript:;">删除</a></td>
</tr>
<tr>
<td>1</td>
<td>李四</td>
<td>22</td>
<td><a href="javascript:;">删除</a></td>
</tr>
<tr>
<td>3</td>
<td>王麻子</td>
<td>47</td>
<td><a href="javascript:;">删除</a></td>
</tr>
</tbody>
</table>
name:<input type="text" id="name">
age:<input type="text" id="age">
<input type="button" value="done" id='done'>
</body>
</html>
网友评论