JS Dom deletes rows and cells, swapping the positions of two rows

Source: Internet
Author: User

This article provides two examples of the DOM operation table, we use the JS DOM to delete rows and cells, swap two rows of location Oh, the following example has been said very tomorrow.

Add Delete Row, table

<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<title>www.111cn.net</title>
<body>
<table id= "table1" bordercolor= "#000000" width= "" border= "1" >
<tbody>
<tr>
<td>00</td>
<td>01</td>
<td>02</td>
<td>03</td>
</tr>
<tr>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
</tr>
<tr>
<td>20</td>
<td>21</td>
<td>22</td>
<td>23</td>
</tr>
<tr>
<td>30</td>
<td>31</td>
<td>32</td>
<td>33</td>
</tr>
<tr>
<td>40</td>
<td>41</td>
<td>42</td>
<td>43</td>
</tr>
</tbody>
</table>
<br/>
<input type= "button" value= "Delete Row" onclick= "DeleteRow ()"/>&nbsp; &nbsp;
<input type= "button" value= "delete a column" onclick= "DeleteColumn ()"/>
</body>
<script language= "Web Effects" type= "Text/javascript" >
<!--
Get Table objects
var _table=document.getelementbyid ("Table1");

Defines a function to delete the first row
function DeleteRow () {
_table.deleterow (0);
}
Defines a function to delete the first column
function DeleteColumn () {
Deleting a column requires column deletion for each row
for (Var i=0;i<_table.rows.length;i++) {
_table.rows[i].deletecell (0);
}
}
-->
</script>

Swap table row Position

<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<title>table test</title>
<style type= "Text/css Tutorial" >
<!--
td {TEXT-ALIGN:CENTER;FONT-SIZE:12PX;PADDING:3PX;}
-->
</style>
<body>
<table id= "table1" bordercolor= "#000000" width= "" border= "1" >
<tbody>
<tr>
&LT;TD width= "25%" >1</td>
&LT;TD width= "25%" >11</td>
<!--using javascript:void (0) is to be able to pass this parameter to an event handler-->
&LT;TD width= "25%" ><a href= "javascript:void (0)" onclick= "MoveUp (This)" > Move Up </a></td>
&LT;TD width= "25%" ><a href= "javascript:void (0)" onclick= "MoveDown (This)" > Move Down </a></td>
</tr>
<tr>
<td>2</td>
<td>22</td>
<td><a href= "javascript:void (0)" onclick= "MoveUp (This)" > Move Up </a></td>
<td><a href= "javascript:void (0)" onclick= "MoveDown (This)" > Move Down </a></td>
</tr>
<tr>
<td>3</td>
<td>33</td>
<td><a href= "javascript:void (0)" onclick= "MoveUp (This)" > Move Up </a></td>
<td><a href= "javascript:void (0)" onclick= "MoveDown (This)" > Move Down </a></td>
</tr>
<tr>
<td>4</td>
<td>44</td>
<td><a href= "javascript:void (0)" onclick= "MoveUp (This)" > Move Up </a></td>
<td><a href= "javascript:void (0)" onclick= "MoveDown (This)" > Move Down </a></td>
</tr>
<tr>
<td>5</td>
<td>55</td>
<td><a href= "javascript:void (0)" onclick= "MoveUp (This)" > Move Up </a></td>
<td><a href= "javascript:void (0)" onclick= "MoveDown (This)" > Move Down </a></td>
</tr>
</tbody>
</table>
</body>

<script language= "javascript" type= "Text/javascript" >
<!--
function Cleanwhitespace (Element) {
Traversing the child nodes of an element
for (var i = 0; i < element.childnodes.length; i++) {
var node = element.childnodes[i];
Determine if the node is a blank text node and, if so, delete it.
if (Node.nodetype = = 3 &&!/s/.test (node.nodevalue))
Node.parentnode.removechild (node);
}
}
Get Table objects
var _table=document.getelementbyid ("Table1");
Cleanwhitespace (_table);
Move the table row up and receive the parameter as a linked object
function MoveUp (_a) {
Get a reference to a table row from a linked object
var _row=_a.parentnode.parentnode;
If not the first line, the order is exchanged with the previous line
if (_row.previoussibling) SwapNode (_row,_row.previoussibling);
}
Moves the table row down and receives the parameter as a linked object
function MoveDown (_a) {
Get a reference to a table row from a linked object
var _row=_a.parentnode.parentnode;
If not the last line, the order is exchanged with the next line
if (_row.nextsibling) SwapNode (_row,_row.nextsibling);
}
Defines the position of a generic function to exchange two nodes
function SwapNode (node1,node2) {
Get parent Node
var _parent=node1.parentnode;
Gets the relative position of two nodes
var _t1=node1.nextsibling;
var _t2=node2.nextsibling;
Insert Node2 into the original Node1 position
if (_T1) _parent.insertbefore (NODE2,_T1);
else _parent.appendchild (NODE2);
Insert Node1 into the original node2 position
if (_t2) _parent.insertbefore (NODE1,_T2);
else _parent.appendchild (Node1);
}
-->
</script>

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.