JS adds tbody blocks to the table to facilitate the addition and deletion of whole blocks

Source: Internet
Author: User

The use of JS in the table to add tbody block, convenient to add and delete the whole block, the following is added.
<table width= "border=" 1 "cellspacing=" 0 "cellpadding=" 0 "id=" table1 "></table><script language=" JavaScript ">var value =" Content ", var value1 =" <a href= ' * ' >link</a> "; var row = 5; Create 5 rows var col = 7; Create 7-grid var tbody = document.createelement ("tbody"); Create a new element node of type Tbody
var tbody1=document.createattribute ("Tbody1");
for (Var i=0;i<row;i++) {var tr = document.createelement ("tr");//Create a new element node for the TR type for (Var j=0;j<col;j++) c1/>{ var td = Document.createelement ("TD");//Create a new element node for TD type td.innerhtml = value1;//link .... Tr.appendchild (TD);//Add node TD to TR Tbody.appendchild (TR); Add the node tr to tbody}var Parnode = document.getElementById ("table1"); Locate the Parnode.appendchild (TBODY) on the table; Add node tbody to node tail </script>
Once you add a TR, you can use the following
<script>var i=0,arr=new Array (' f9f9f9 ', ' f0f0f0 '); function AddNew () {
var parnode = document.getElementById ("table1"); Navigate to Table Tr=parnode. InsertRow (); tr.style.backgroundcolor=arr[i%2];

Tr.insertcell (). innertext= "Picture" + (++i);
Tr.insertcell (). innerhtml= ' <input type=file name=pic ' +i+ ' class= "Border_index" > <a href=javascript:void (0) Onclick=del () > Delete </a> '
}}
The whole block is only required to remove
<script language= "JavaScript" >
var parnode = document.getElementById ("table1"); Navigate to Table
var tbody;
if (-[1,]) {//Non IE
Tbody=document.getelementsbytagname ("tbody") [0];

}else{//ie
Tbody=document.getelementsbytagname ("tbody") [0];

}
Deleting one TR line at a time can be done in the following ways
<script language= "JavaScript" >
var Parnode=document.getelementbyid ("Table1");
Parnode.deleterow (2);//Remove the third row TR from the table


</script>

JS adds tbody blocks to the table to facilitate the addition and deletion of whole blocks

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.