Use JavaScript to move rows up and down

Source: Internet
Author: User

Http://sunxboy.javaeye.com/blog/191652

 

 

Use JavaScript to move up and down rows

Keyword: Row MovementHTML code

  1. <! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
  2. <HTML>
  3. <Head>
  4. <Title> table test </title>
  5. <Style type = "text/CSS">
  6. <! --
  7. TD {text-align: center; font-size: 12px; padding: 3px ;}
  8. -->
  9. </Style>
  10. </Head>
  11. <Body>
  12. <Table id = "Table1" bordercolor = "#000000" width = "200" border = "1">
  13. <Tbody>
  14. <Tr>
  15. <TD width = "25%" type = "codeph" text = "codeph"> 1 </TD type = "codeph" text = "/codeph">
  16. <TD width = "25%"> 11 </TD>
  17. <! -- Javascript: void (0) is used to pass this parameter to the Event Handler -->
  18. <TD width = "25%"> <a href = "javascript: void (0)" onclick = "moveup (this)"> move up </a> </TD>
  19. <TD width = "25%"> <a href = "javascript: void (0)" onclick = "movedown (this)"> move down </a> </TD>
  20. </Tr>
  21. <Tr>
  22. <TD> 2 </TD>
  23. <TD> 22 </TD>
  24. <TD> <a href = "javascript: void (0)" onclick = "moveup (this)"> move up </a> </TD>
  25. <TD> <a href = "javascript: void (0)" onclick = "movedown (this)"> move down </a> </TD>
  26. </Tr>
  27. <Tr>
  28. <TD> 3 </TD>
  29. <TD> 33 </TD>
  30. <TD> <a href = "javascript: void (0)" onclick = "moveup (this)"> move up </a> </TD>
  31. <TD> <a href = "javascript: void (0)" onclick = "movedown (this)"> move down </a> </TD>
  32. </Tr>
  33. <Tr>
  34. <TD> 4 </TD>
  35. <TD> 44 </TD>
  36. <TD> <a href = "javascript: void (0)" onclick = "moveup (this)"> move up </a> </TD>
  37. <TD> <a href = "javascript: void (0)" onclick = "movedown (this)"> move down </a> </TD>
  38. </Tr>
  39. <Tr>
  40. <TD> 5 </TD>
  41. <TD> 55 </TD>
  42. <TD> <a href = "javascript: void (0)" onclick = "moveup (this)"> move up </a> </TD>
  43. <TD> <a href = "javascript: void (0)" onclick = "movedown (this)"> move down </a> </TD>
  44. </Tr>
  45. </Tbody>
  46. </Table>
  47. </Body>
  48. </Html>
  49. <Script language = "JavaScript" type = "text/JavaScript">
  50. <! --
  51. Function cleanwhitespace (element ){
  52. // Traverse the child nodes of the element
  53. For (VAR I = 0; I <element. childnodes. length; I ++ ){
  54. VaR node = element. childnodes [I];
  55. // Determine whether the node is a blank text node. If yes, delete the node.
  56. If (node. nodetype = 3 &&! /\ S/. Test (node. nodevalue ))
  57. Node. parentnode. removechild (node );
  58. }
  59. }
  60. // Obtain the table object
  61. VaR _ TABLE = Document. getelementbyid ("Table1 ");
  62. Cleanwhitespace (_ table );
  63. // Move the table row up. The receiving parameter is a link object.
  64. Function moveup (_ ){
  65. // Obtain table row Reference through link object
  66. VaR _ ROW = _ A. parentnode. parentnode;
  67. // If it is not the first line, it is exchanged with the previous line.
  68. If (_ row. previussibling) swapnode (_ row, _ row. previussibling );
  69. }
  70. // Move the table row down. The receiving parameter is a link object.
  71. Function movedown (_ ){
  72. // Obtain table row Reference through link object
  73. VaR _ ROW = _ A. parentnode. parentnode;
  74. // If it is not the last row, it is exchanged with the next row.
  75. If (_ row. nextsibling) swapnode (_ row, _ row. nextsibling );
  76. }
  77. // Define the location of two common function switches
  78. Function swapnode (node1, node2 ){
  79. // Obtain the parent node
  80. VaR _ parent = node1.parentnode;
  81. // Obtain the relative location of the two nodes
  82. VaR _ T1 = node1.nextsibling;
  83. VaR _ t2 = node2.nextsibling;
  84. // Insert node2 to the original location of node1.
  85. If (_ T1) _ parent. insertbefore (node2, _ T1 );
  86. Else _ parent. appendchild (node2 );
  87. // Insert node1 to the original location of node2.
  88. If (_ T2) _ parent. insertbefore (node1, _ T2 );
  89. Else _ parent. appendchild (node1 );
  90. }
  91. // -->
  92. </SCRIPT>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en"> <HTML> 

  • Description: Row movement, compatible with IE/Firefox
  • Size: 2 kb

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.