在html表中上下移动行

时间:2010-12-01 04:56:51

标签: javascript html

  

可能重复:
  Swapping rows in JQuery

如何在某个按钮点击时在表格中上下移动行?

3 个答案:

答案 0 :(得分:7)

尝试使用jQuery。你可以这样做:

<table id="mytable">
    <tr>
        <td>row 1</td>
        <td><input type="button" value="move up" class="move up" /></td>
        <td><input type="button" value="move down" class="move down" /></td>
    </tr>
    ...
</table>

$('#mytable input.move').click(function() {
    var row = $(this).closest('tr');
    if ($(this).hasClass('up'))
        row.prev().before(row);
    else
        row.next().after(row);
});

看看它有效here

你也可以试试jQuery的sortable,它是much easier

答案 1 :(得分:2)

$sel = "SELECT emp_name from emps";
$res = mysql_query($sel);
?>
<form id="ffff">
 <table border="1">
 <?php
      $c =0;
      while($row = mysql_fetch_object($res)) {
       $c++;
 ?>
       <tr>
           <td><div id="D<?php echo $c; ?>R"><?php echo $row->emp_name; ?></div></td>
           <td><input type="button" id="D<?php echo $c; ?>" value="Down" class="move down"  onClick="MoveDown('D<?php echo $c; ?>','D<?php echo $c+1; ?>');"/></td>
           <td><input type="button" id="U<?php echo $c; ?>" value="Up" class="move up"  onClick="MoveUp('D<?php echo $c; ?>','D<?php echo $c-1; ?>');"/></td>
       </tr>
 <?php
      }
 ?>
 </table>
</form>
<script type="text/javascript">
    function MoveDown(roww,rowwN)
    { 
      divv = document.getElementById(roww+'R').innerHTML;
      if(roww)
      { 
        ch = document.getElementById(rowwN+'R').innerHTML;
        document.getElementById(rowwN+'R').innerHTML = document.getElementById(roww+'R').innerHTML;
        document.getElementById(roww+'R').innerHTML = ch; 
      }
    }
    function MoveUp(roww,rowwN)
    { 
      divv = document.getElementById(roww+'R').innerHTML;
      if(roww)
      { 
        ch = document.getElementById(rowwN+'R').innerHTML;
        document.getElementById(rowwN+'R').innerHTML = document.getElementById(roww+'R').innerHTML;
        document.getElementById(roww+'R').innerHTML = ch; 
      }
    }
</script>

答案 2 :(得分:1)

<table id="tableID" border="1" width="400">
        <tr id="1">
                <td>1</td><td>One<input type="button" class="up" value="up"/><input type="button" class="down" value="down"/></td>
        </tr>
        <tr id="2">
                <td>2</td><td>Two<input type="button" class="up" value="up"/><input type="button" class="down" value="down"/></td>
        </tr>
        <tr id="3">
                <td>3</td><td>Three<input type="button" class="up" value="up"/><input type="button" class="down" value="down"/></td>
        </tr>

</table>
<script type="text/javascript">
$(document).ready(function(){
    $("#tableID").delegate('input.up','click', function(e) {

        var it = $(this).closest('tr');
        var prev = $(this).closest('tr').prev('tr');

        if(it.attr("id") != $("tr:first").attr("id")){
            it.remove();
            it.insertBefore(prev);
        }   
    });
    $("#tableID").delegate('input.down','click', function(e) {

        var it = $(this).closest('tr');
        var next = $(this).closest('tr').next('tr');

        if(it.attr("id") != $("tr:last").attr("id")){
            it.remove();
            it.insertAfter(next);
        }           
    });
});
</script>