Js實(shí)現(xiàn)動態(tài)添加刪除Table行示例
更新時(shí)間:2014年04月14日 15:07:04 作者:
這篇文章主要介紹了Js動態(tài)添加刪除Table行的具體實(shí)現(xiàn),需要的朋友可以參考下
最近做項(xiàng)目遇到要?jiǎng)討B(tài)添加、刪除表格行的操作,實(shí)現(xiàn)如下
html代碼
<table cellpadding="0" cellspacing="0" border="1" style="margin:auto; width:96%;" id="LearnInfoItem">
<tr >
<td colspan="8" bgcolor="#96E0E2" style="height:30px;" ><h3 style="text-align:center; margin:0;">主要學(xué)習(xí)簡歷</h3></td>
</tr>
<tr id="tr1">
<td class="tdStyle2">起訖時(shí)間 </td>
<td class="tdStyle2">畢業(yè)院校</td>
<td class="tdStyle2">所學(xué)專業(yè)</td>
<td class="tdStyle2">學(xué)制</td>
<td class="tdStyle2">學(xué)位</td>
<td class="tdStyle2">學(xué)習(xí)方式</td>
<td class="tdStyle2">文化程度</td>
<td class="tdStyle2">
<input type="button" name="LearnAdd" value="添加" onclick="LearnAddSignRow()" />
<input name='LearnTRLastIndex' type='hidden' id='LearnTRLastIndex' value="1" />
</td>
</tr>
</table>
javascript代碼:
<script language="javascript" type="text/javascript">// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
var p, i, foundObj;
if(!theDoc) theDoc = document;
if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
{
theDoc = parent.frames[theObj.substring(p+1)].document;
theObj = theObj.substring(0,p);
}
if(!(foundObj = theDoc[theObj]) && theDoc.all)
foundObj = theDoc.all[theObj];
for (i=0; !foundObj && i < theDoc.forms.length; i++)
foundObj = theDoc.forms[i][theObj];
for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
foundObj = findObj(theObj,theDoc.layers[i].document);
if(!foundObj && document.getElementById)
foundObj = document.getElementById(theObj);
return foundObj;
}
//添加一行學(xué)習(xí)簡歷
function LearnAddSignRow(){ //讀取最后一行的行號,存放在LearnTRLastIndex文本框中
var LearnTRLastIndex = findObj("LearnTRLastIndex",document);
var rowID = parseInt(LearnTRLastIndex.value);
var signFrame = findObj("LearnInfoItem",document);
//添加行
var newTR = signFrame.insertRow(signFrame.rows.length);
newTR.id = "LearnItem" + rowID;
//添加列:起訖時(shí)間
var newNameTD=newTR.insertCell(0);
//添加列內(nèi)容
newNameTD.innerHTML = "<input name='txtLearnStartDate" + rowID + "' id='txtLearnStartDate" + rowID + "' type='text' class='inputStyle' />";
//添加列:畢業(yè)院校
var newNameTD=newTR.insertCell(1);
//添加列內(nèi)容
newNameTD.innerHTML = "<input name='txtName" + rowID + "' id='txtName" + rowID + "' type='text' class='inputStyle' />";
//添加列:所學(xué)專業(yè)
var newEmailTD=newTR.insertCell(2);
//添加列內(nèi)容
newEmailTD.innerHTML = "<input name='txtEMail" + rowID + "' id='txtEmail" + rowID + "' type='text' class='inputStyle' />";
//添加列:學(xué)制
var newTelTD=newTR.insertCell(3);
//添加列內(nèi)容
newTelTD.innerHTML = "<input name='txtTel" + rowID + "' id='txtTel" + rowID + "' type='text' class='inputStyle' />";
//添加列:學(xué)位
var newMobileTD=newTR.insertCell(4);
//添加列內(nèi)容
newMobileTD.innerHTML = "<input name='txtMobile" + rowID + "' id='txtMobile" + rowID + "' type='text' class='inputStyle' />";
//添加列:學(xué)習(xí)方式
var newMobileTD=newTR.insertCell(5);
//添加列內(nèi)容
newMobileTD.innerHTML = "<input name='txtMobile" + rowID + "' id='txtMobile" + rowID + "' type='text' class='inputStyle' />";
//添加列:文化程度
var newCompanyTD=newTR.insertCell(6);
//添加列內(nèi)容
newCompanyTD.innerHTML = "<input name='txtCompany" + rowID + "' id='txtCompany" + rowID + "' type='text' class='inputStyle' />";
//添加列:刪除按鈕
var newDeleteTD=newTR.insertCell(7);
//添加列內(nèi)容
newDeleteTD.innerHTML = "<div align='center'><input id='txtDel" + rowID + "' type='button' value='刪除' onclick=\"LearnDeleteRow('LearnItem" + rowID + "')\" class='inputStyle' /></div>";
//將行號推進(jìn)下一行
LearnTRLastIndex.value = (rowID + 1).toString() ;
}
//刪除指定行
function LearnDeleteRow(rowid){
var signFrame = findObj("LearnInfoItem",document);
var signItem = findObj(rowid,document);
//獲取將要?jiǎng)h除的行的Index
var rowIndex = signItem.rowIndex;
//刪除指定Index的行
signFrame.deleteRow(rowIndex);
}
}
</script>
實(shí)現(xiàn)效果:
html代碼
復(fù)制代碼 代碼如下:
<table cellpadding="0" cellspacing="0" border="1" style="margin:auto; width:96%;" id="LearnInfoItem">
<tr >
<td colspan="8" bgcolor="#96E0E2" style="height:30px;" ><h3 style="text-align:center; margin:0;">主要學(xué)習(xí)簡歷</h3></td>
</tr>
<tr id="tr1">
<td class="tdStyle2">起訖時(shí)間 </td>
<td class="tdStyle2">畢業(yè)院校</td>
<td class="tdStyle2">所學(xué)專業(yè)</td>
<td class="tdStyle2">學(xué)制</td>
<td class="tdStyle2">學(xué)位</td>
<td class="tdStyle2">學(xué)習(xí)方式</td>
<td class="tdStyle2">文化程度</td>
<td class="tdStyle2">
<input type="button" name="LearnAdd" value="添加" onclick="LearnAddSignRow()" />
<input name='LearnTRLastIndex' type='hidden' id='LearnTRLastIndex' value="1" />
</td>
</tr>
</table>
javascript代碼:
復(fù)制代碼 代碼如下:
<script language="javascript" type="text/javascript">// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
var p, i, foundObj;
if(!theDoc) theDoc = document;
if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
{
theDoc = parent.frames[theObj.substring(p+1)].document;
theObj = theObj.substring(0,p);
}
if(!(foundObj = theDoc[theObj]) && theDoc.all)
foundObj = theDoc.all[theObj];
for (i=0; !foundObj && i < theDoc.forms.length; i++)
foundObj = theDoc.forms[i][theObj];
for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
foundObj = findObj(theObj,theDoc.layers[i].document);
if(!foundObj && document.getElementById)
foundObj = document.getElementById(theObj);
return foundObj;
}
//添加一行學(xué)習(xí)簡歷
function LearnAddSignRow(){ //讀取最后一行的行號,存放在LearnTRLastIndex文本框中
var LearnTRLastIndex = findObj("LearnTRLastIndex",document);
var rowID = parseInt(LearnTRLastIndex.value);
var signFrame = findObj("LearnInfoItem",document);
//添加行
var newTR = signFrame.insertRow(signFrame.rows.length);
newTR.id = "LearnItem" + rowID;
//添加列:起訖時(shí)間
var newNameTD=newTR.insertCell(0);
//添加列內(nèi)容
newNameTD.innerHTML = "<input name='txtLearnStartDate" + rowID + "' id='txtLearnStartDate" + rowID + "' type='text' class='inputStyle' />";
//添加列:畢業(yè)院校
var newNameTD=newTR.insertCell(1);
//添加列內(nèi)容
newNameTD.innerHTML = "<input name='txtName" + rowID + "' id='txtName" + rowID + "' type='text' class='inputStyle' />";
//添加列:所學(xué)專業(yè)
var newEmailTD=newTR.insertCell(2);
//添加列內(nèi)容
newEmailTD.innerHTML = "<input name='txtEMail" + rowID + "' id='txtEmail" + rowID + "' type='text' class='inputStyle' />";
//添加列:學(xué)制
var newTelTD=newTR.insertCell(3);
//添加列內(nèi)容
newTelTD.innerHTML = "<input name='txtTel" + rowID + "' id='txtTel" + rowID + "' type='text' class='inputStyle' />";
//添加列:學(xué)位
var newMobileTD=newTR.insertCell(4);
//添加列內(nèi)容
newMobileTD.innerHTML = "<input name='txtMobile" + rowID + "' id='txtMobile" + rowID + "' type='text' class='inputStyle' />";
//添加列:學(xué)習(xí)方式
var newMobileTD=newTR.insertCell(5);
//添加列內(nèi)容
newMobileTD.innerHTML = "<input name='txtMobile" + rowID + "' id='txtMobile" + rowID + "' type='text' class='inputStyle' />";
//添加列:文化程度
var newCompanyTD=newTR.insertCell(6);
//添加列內(nèi)容
newCompanyTD.innerHTML = "<input name='txtCompany" + rowID + "' id='txtCompany" + rowID + "' type='text' class='inputStyle' />";
//添加列:刪除按鈕
var newDeleteTD=newTR.insertCell(7);
//添加列內(nèi)容
newDeleteTD.innerHTML = "<div align='center'><input id='txtDel" + rowID + "' type='button' value='刪除' onclick=\"LearnDeleteRow('LearnItem" + rowID + "')\" class='inputStyle' /></div>";
//將行號推進(jìn)下一行
LearnTRLastIndex.value = (rowID + 1).toString() ;
}
//刪除指定行
function LearnDeleteRow(rowid){
var signFrame = findObj("LearnInfoItem",document);
var signItem = findObj(rowid,document);
//獲取將要?jiǎng)h除的行的Index
var rowIndex = signItem.rowIndex;
//刪除指定Index的行
signFrame.deleteRow(rowIndex);
}
}
</script>
實(shí)現(xiàn)效果:
您可能感興趣的文章:
- JS 排序輸出實(shí)現(xiàn)table行號自增前端動態(tài)生成的tr
- JS動態(tài)添加Table的TR,TD實(shí)現(xiàn)方法
- JavaScript如何動態(tài)創(chuàng)建table表格
- js動態(tài)給table添加/刪除tr的方法
- JS動態(tài)創(chuàng)建Table,Tr,Td并賦值的具體實(shí)現(xiàn)
- js實(shí)現(xiàn)對table動態(tài)添加、刪除和更新的方法
- JS實(shí)現(xiàn)動態(tài)修改table及合并單元格的方法示例
- jQuery+json實(shí)現(xiàn)動態(tài)創(chuàng)建復(fù)雜表格table的方法
- javascript DOM操作之動態(tài)刪除TABLE多行
- JS實(shí)現(xiàn)動態(tài)生成html table表格的方法分析
相關(guān)文章
在VS?Code中使用Snippet?Craft擴(kuò)展提高編碼效率的過程詳解
這篇文章主要介紹了在VS?Code中使用Snippet?Craft擴(kuò)展提高編碼效率,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08
增強(qiáng)的 JavaScript 的 trim 函數(shù)的代碼
增強(qiáng)的 JavaScript 的 trim 函數(shù)的代碼...2007-08-08
微信小程序?qū)崿F(xiàn)通過js操作wxml的wxss屬性示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)通過js操作wxml的wxss屬性,結(jié)合實(shí)例形式分析了微信小程序使用js操作wxml的wxss屬性相關(guān)原理、實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2018-12-12
一個(gè)簡單不報(bào)錯(cuò)的summernote 圖片上傳案例
下面小編就為大家?guī)硪黄粋€(gè)簡單不報(bào)錯(cuò)的summernote圖片上傳案例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07
JavaScript純前端實(shí)現(xiàn)在線GIF壓縮
這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript純前端實(shí)現(xiàn)在線GIF壓縮工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
JavaScript?中的運(yùn)算符和表達(dá)式介紹(二)
這篇文章主要介紹了JavaScript?中的運(yùn)算符和表達(dá)式介紹,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09

