<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<body>
<table width= "border=" 1 "id=" TB1 ">
<tr>
<td> title </td>
<td> Price </td>
</tr>
<tr>
<td>21 days proficient in java</td>
<td>99</td>
</tr>
<tr>
<td>21 days proficient in c#</td>
<td>99</td>
</tr>
<tr>
<td>21 days proficient in jsp</td>
<td>99</td>
</tr>
</table>
<button onclick= "AddRow (); > Add a line </button>
<button onclick= "Delrow (); > Delete last line </button>
<button onclick= "Chgstyle (); > Modify Heading Styles </button>
<button onclick= "Clonerow (); > Copy last line </button>
<script>
Get Object
function Getel (ID) {
return document.getElementById (ID);
}
Find objects by tag name in an object
function Getelbytag (Obj,tag) {
return Obj.getelementsbytagname (tag);
}
Creating objects
function Createel (tag) {
return document.createelement (tag);
}
var Tb=getel ("tb1");//Get Table Object
var tbody=getelbytag (TB, "tbody") [0];//gets Tbody object
Copy last line
function Clonerow () {
var trs=getelbytag (TB, "tr");//Get all rows
var tr=trs[trs.length-1];//get the last row
var newtr=tr.clonenode (true);//Clone a new line
Tbody.appendchild (NEWTR);//Add New Line
}
Modify the style of a table header
function Chgstyle () {
var trs=getelbytag (TB, "tr");//Get all rows
var tr=trs[0];//get header row
Tr.style.backgroundcolor= "#ccc";//Modify style-background color
}
Delete last line
function Delrow () {
var trs=getelbytag (TB, "tr");//Get all rows
if (trs.length==1) {//If there is only one row
Alert ("Only table header, cannot delete!");
Return
}
var tr=trs[trs.length-1];//get the last row
Tbody.removechild (TR);//delete rows
}
Increase row
function AddRow () {
var tr=createel ("tr");//Create Line Object
var td1=createel ("TD");//Create first column
var td2=createel ("TD");//Create second column
Td1.innerhtml= "test";//Fill in the first column of content
Td2.innerhtml= "88";//Fill in the second column
Assembly
Tr.appendchild (TD1);//assemble the first column
Tr.appendchild (TD2);//assemble the second column
Tbody.appendchild (TR);//assembly line
}
</script>
</body>
Add a row