Javascript 在html表格中上下移动行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4321320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:53:36 来源:igfitidea点击:
Move Rows up and down in html table
提问by Manthan
Possible Duplicate:
Swapping rows in JQuery
可能的重复:
在 JQuery 中交换行
How to move rows up and down in a table on some button click?
如何在单击某些按钮时在表格中上下移动行?
回答by cambraca
Try using jQuery. You can do something like:
尝试使用 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);
});
Look at it working here.
看看它在这里工作。
You can also try jQuery's sortable, it is much easier.
回答by Sankar
$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>
回答by noname260588
<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>