html表格动态添加两行实例,Js实现动态添加删除Table行示例

这篇博客介绍了如何使用JavaScript实现HTML表格的动态添加和删除行功能。通过`findObj`函数查找DOM元素,然后在`LearnInfoItem`表格中插入或删除行,包括起讫时间、毕业院校、所学专业等字段。代码详细展示了添加新行并填充默认内容,以及根据行ID删除指定行的过程。
摘要由CSDN通过智能技术生成

最近做项目遇到要动态添加、删除表格行的操作,实现如下

html代码

主要学习简历

起讫时间 毕业院校所学专业学制学位学习方式文化程度

javascript代码:

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;

}

//添加一行学习简历

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;

//添加列:起讫时间

var newNameTD=newTR.insertCell(0);

//添加列内容

newNameTD.innerHTML = "";

//添加列:毕业院校

var newNameTD=newTR.insertCell(1);

//添加列内容

newNameTD.innerHTML = "";

//添加列:所学专业

var newEmailTD=newTR.insertCell(2);

//添加列内容

newEmailTD.innerHTML = "";

//添加列:学制

var newTelTD=newTR.insertCell(3);

//添加列内容

newTelTD.innerHTML = "";

//添加列:学位

var newMobileTD=newTR.insertCell(4);

//添加列内容

newMobileTD.innerHTML = "";

//添加列:学习方式

var newMobileTD=newTR.insertCell(5);

//添加列内容

newMobileTD.innerHTML = "";

//添加列:文化程度

var newCompanyTD=newTR.insertCell(6);

//添加列内容

newCompanyTD.innerHTML = "";

//添加列:删除按钮

var newDeleteTD=newTR.insertCell(7);

//添加列内容

newDeleteTD.innerHTML = "

";

//将行号推进下一行

LearnTRLastIndex.value = (rowID + 1).toString() ;

}

//删除指定行

function LearnDeleteRow(rowid){

var signFrame = findObj("LearnInfoItem",document);

var signItem = findObj(rowid,document);

//获取将要删除的行的Index

var rowIndex = signItem.rowIndex;

//删除指定Index的行

signFrame.deleteRow(rowIndex);

}

}

实现效果:

c406763ac04e583c96c47bfef0b5436f.gif

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值