基于javascipt-dom編程 table對(duì)象的使用
排名練習(xí):向表格添加數(shù)據(jù),當(dāng)編號(hào)遇到重復(fù)給予提示并且無法添加,而且按排名順序添加數(shù)據(jù)
參考代碼:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>ggggg</title>
<link rel="stylesheet" type="text/css" href="">
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<!--js代碼可以放置在任意位置,按照先后順序依次執(zhí)行 一般放在head標(biāo)簽之間-->
<script type="text/javascript">
function test(){
//獲得表格對(duì)象
var mytable=document.getElementById("table1");
//判斷插入是否重復(fù)的排名
//遍歷
//定義變量要插入的位置
var insertW=mytable.rows.length;
for(var i=1;i<mytable.rows.length;i++){
//取出每一行
var eRows=mytable.rows[i];
//判斷輸入的排名是否重復(fù)
if(eRows.cells[0].innerText==no.value){
window.alert("與排名 "+eRows.cells[0].innerText+" 重復(fù)");
return ;//阻止代碼往下面執(zhí)行
}
//進(jìn)行排序 查找要插入的位置
if(parseInt(no.value)>parseInt(eRows.cells[0].innerText)){
insertW=i;
//window.alert(insertW);
}
//window.alert("ok");
}
//排序后的插入
var eachRow=mytable.insertRow(insertW+1);
//每行添加數(shù)據(jù)
eachRow.insertCell(0).innerText=document.getElementById("no").value;
eachRow.insertCell(1).innerText=username.value;
eachRow.insertCell(2).innerText=nickname.value;
}
</script>
</head>
<body>
<table id="table1" border="1">
<tr><td>排名</td><td>姓名</td><td>外號(hào)</td></tr>
<tr><td>1</td><td>宋江</td><td>及時(shí)雨</td></tr>
<tr><td>2</td><td>盧俊義</td><td>玉麒麟</td></tr>
<tr><td>10</td><td>test</td><td>test</td></tr>
</table>
<br/><br/>
排名:<input id="no" type="text" name="no"><br/>
姓名:<input id="username" type="text" name="username"><br/>
外號(hào):<input id="nickname" type="text" name="nickname"><br/>
<input type="button" value="添加" onclick="test()"><br/>
</body>
</html>
相關(guān)文章
iframe 上下滾動(dòng)條如何默認(rèn)在下方實(shí)現(xiàn)原理
iframe 上下滾動(dòng)條如何默認(rèn)在下方,做的是聊天工具,數(shù)據(jù)多了,每次刷新出現(xiàn)的上下滾動(dòng)默認(rèn)在上方,還需下拉到下面才能看到聊天記錄,本文將介紹,如和實(shí)現(xiàn)在下方2012-12-12深入Javascript函數(shù)、遞歸與閉包(執(zhí)行環(huán)境、變量對(duì)象與作用域鏈)使用詳解
本篇文章對(duì)Javascript中函數(shù)、遞歸與閉包(執(zhí)行環(huán)境、變量對(duì)象與作用域鏈)的使用進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05Javascript & DHTML 實(shí)例編程(教程)基礎(chǔ)知識(shí)
Javascript & DHTML 實(shí)例編程(教程)基礎(chǔ)知識(shí)...2007-06-06在javascript中關(guān)于節(jié)點(diǎn)內(nèi)容加強(qiáng)
本篇文章,小編為大家介紹,關(guān)于在javascript中關(guān)于節(jié)點(diǎn)內(nèi)容加強(qiáng)的問題2013-04-04