JavaScript implementation of table additions and deletions implementation code

Source: Internet
Author: User

Example 1

The code is as follows Copy Code

<script>
function AddRow () {

Insert a row
Mynewrow = Document.all.myTable.insertRow ();
var lenrow = document.all.myTable.rows.length; Calculate total number of rows

if (Lenrow > 1) {
var point = Mynewrow.rowindex; Calculate Current line Position
var lencol = document.all.myTable.rows (0). Cells.length; Calculate a few columns per row

Insert Lencol TD
for (i=0 i < Lencol; i++) {
Document.all.myTable.rows (point). InsertCell ();
Document.all.myTable.rows (point). Cells (i). InnerHTML = parsefloat (Document.all.myTable.rows (point-1). Cells (i). InnerHTML) + Lencol;
}
}else if (Lenrow = = 1) {
The original table is 0 rows, initializing a row
for (i = 0; i < i++) {
Document.all.myTable.rows (0). InsertCell ();
Document.all.myTable.rows (0). Cells (i). InnerHTML = i;
}
}
}

function Delrow () {
Document.all.myTable.deleteRow ();
}
</script>
<a href= "Javascript:addrow ()" > Add one line </a>
<a href= "Javascript:delrow ()" > Minus one line </a>
<table name=mytable border=1 id=mytable>
</table>

Example 2

The code is as follows Copy Code

<HTML>
<title>form Object example</title>
<HEAD>
<script language= "JavaScript" >
function Delrow1 ()
{
var oelement=event.srcelement;
while (oelement.tagname!= "TR")
{
Oelement=oelement.parentelement;
}
var otbody=oelement.parentelement;
Otbody.removechild (oelement)
}

function Delrow2 ()//?h except when moving forward
{
var Currrowindex=event.srcelement.parentnode.parentnode.rowindex;
Document.all.yltable.deleteRow (currrowindex);//table10--table ID
}
function Insertrow1 ()//Added one-line Method 1
{
var newnode = document.getElementById (' yltable '). Lastchild.clonenode (True);
document.getElementById (' yltable '). appendchild (NewNode);
}

function Insertrow2 ()//Added one-line Method 2
{
Newrow=document.all.yltable.insertrow (-1);
var j_1 = document.all.yltable.rows.length;
Newcell=newrow.insertcell ();
Newrow.bgcolor= ' #FFFFFF ';
newcell.align= ' center ';
Newcell.innerhtml= "" +j_1+ "";


Newcell=newrow.insertcell ();
Newrow.bgcolor= ' #FFFFFF ';

newcell.align= ' center ';
Newcell.innerhtml= "<input type= ' text ' name= ' yltext ' +j_1+" '/> ";


Newcell=newrow.insertcell ();
Newrow.bgcolor= ' #FFFFFF ';

newcell.align= ' center ';

Newcell.innerhtml= ' <input name= button3 "type=" button "onclick=" Delrow1 () "value=" Delete 1 "> <input name=" Button3 "type=" button "onclick=" Delrow2 () "value=" Delete 2 ">";

Document.all.yltable.focus ();

}

function inserttable ()
{
var newnode = document.getElementById (' yltable '). CloneNode (True);
document.getElementById (' Ylform '). appendchild (NewNode);
}
</script>
</HEAD>
<BODY>
<form name= "Ylform" id= "Ylform" >
<center>
<input name= "button" type= "button" onclick= "Inserttable ()" value= "Add Table" >
<input name= "button2" type= "button" onclick= "Insertrow1 ()" value= "Add line 1" >

<input name= "button22" type= "button" onclick= "Insertrow2 ()" value= "Add line 2" >
</center>


<div style= "Overflow-y:auto; overflow-x:visible;width:85%;height:150px ">
<table border= "1" align= "center" width= "50%" id= "yltable" >
<tbody id=yl1>
<tr>
&LT;TD width= "30%" height= "><div align=" center ">1</div></td>
&LT;TD width= "40%" >
<div align= "center" >
<input name= "TextField" type= "text" value= "YL" >
</div></td>
&LT;TD width= "30%" ><div align= "center" >
<input name= "Button3" type= "button" onclick= "Delrow1 ()" value= "Delete 1" > <input name= "button3" type= "button" Onclick= "Delrow2 ()" value= "Delete 2" >
</div></td>
</tr>
</TBODY>
<tbody id=yl2>
<tr>
<td><div align= "center" >2</div></td>
<td><div align= "center" >
<input name= "Textfield2" type= "text" value= "Yanleigis" >
</div></td>
<td><div align= "center" >
<input name= "Button3" type= "button" onclick= "Delrow1 ()" value= "Delete 1" > <input name= "button3" type= "button" Onclick= "Delrow2 ()" value= "Delete 2" >
</div></td>
</tr>
</TBODY>
<tbody id=yl3>
<tr>
&LT;TD ><div align= "center" >3</div></td>
&LT;TD ><div align= "center" >
<input name= "Textfield22" type= "text" value= "Landgis@126.com" >
</div></td>
&LT;TD ><div align= "center" >
<input name= "Button3" type= "button" onclick= "Delrow1 ()" value= "Delete 1" > <input name= "button3" type= "button" Onclick= "Delrow2 ()" value= "Delete 2" >
</div></td>
</tr>
</TBODY>
</table>
</div>


<center>
</center>
</form>
</BODY>
</HTML>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.