Add a row

Source: Internet
Author: User

<! 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

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.