本文主要是介绍JS实现上移下移功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
下面代码可直接运行
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> 上移下移</title>
<script type="text/javascript">
function test1(id)
{
var row = document.getElementById(id).parentElement.parentElement.rowIndex;
if(row==1){
alert(' 当前最大值,无需上移');
}else{
//上一行的名称列值
var htm1=document.getElementById("tab").rows[row-1].cells[1].innerHTML;
// 当前行的名称列值
var htm2=document.getElementById(id).parentElement.parentElement.cells[1].innerHTML;
document.getElementById("tab").rows[row-1].cells[1].innerHTML=htm2;
document.getElementById(id).parentElement.parentElement.cells[1].innerHTML=htm1;
}
}
function test2(id)
{
//或得到当前行的行号
var row = document.getElementById(id).parentElement.parentElement.rowIndex;
//或得到table的总行数
var count=document.getElementById("tab").rows.length;
//if 当前行的行号等于table的总行数则不进行下移
if(row==(count-1)){
alert('当前最小值,无需下移');
}else{
// 下一行的名称列值
var htm1=document.getElementById("tab").rows[row+1].cells[1].innerHTML;
// 当前行的名称列值
var htm2=document.getElementById(id).parentElement.parentElement.cells[1].innerHTML;
// 当前行的名称列的值为下一行的值
document.getElementById("tab").rows[row+1].cells[1].innerHTML=htm2;
// 下一行的值被赋值为当前行的值
document.getElementById(id).parentElement.parentElement.cells[1].innerHTML=htm1;
}
}
</script>
</head>
<body>
<form action="#" method="post">
<table width="200" border="1" id="tab">
<tr>
<td>序号</td>
<td>名称</td>
<td>编辑</td>
</tr>
<tr>
<td>1</td>
<td>name1</td>
<td>
<input id="up1" type="button" name="up1" οnclick="test1('up1');" value="上移">
<input id="down1" type="button" name="down" onClick="test2('down1');"value="下移">
</td>
</tr>
<tr>
<td>2</td>
<td>name2</td>
<td> <input id="up2" type="button" name="up1" οnclick="test1('up2');" value="上移">
<input id="down2" type="button" name="down" onClick="test2('down2');"value="下移"></td>
</tr>
<tr>
<td>3</td>
<td>name3</td>
<td> <input id="up3" type="button" name="up1" οnclick="test1('up3');" value="上移">
<input id="down3" type="button" name="down" onClick="test2('down3');"value="下移"></td>
</tr>
<tr>
<td>4</td>
<td>name4</td>
<td> <input id="up4" type="button" name="up" οnclick="test1('up4');" value="上移">
<input id="down4" type="button" name="down" onClick="test2('down4');"value="下移"></td>
</tr>
<tr>
<td>5</td>
<td>name5</td>
<td> <input id="up5" type="button" name="up1" οnclick="test1('up5');" value="上移">
<input id="down5" type="button" name="down" onClick="test2('down5');"value="下移"></td>
</tr>
<tr>
<td>6</td>
<td>name6</td>
<td> <input id="up6" type="button" name="up1" οnclick="test1('up6');" value="上移">
<input id="down6" type="button" name="down" onClick="test2('down6');"value="下移"></td>
</tr>
<tr>
<td>7</td>
<td>name7</td>
<td> <input id="up7" type="button" name="up1" οnclick="test1('up7');" value="上移">
<input id="down7" type="button" name="down" onClick="test2('down7');"value="下移"></td>
</tr>
<tr>
<td>8</td>
<td>name8</td>
<td> <input id="up8" type="button" name="up1" οnclick="test1('up8');" value="上移">
<input id="down8" type="button" name="down" onClick="test2('down8');"value="下移"></td>
</tr>
<tr>
<td>9</td>
<td>name9</td>
<td> <input id="up9" type="button" name="up1" οnclick="test1('up9');" value="上移">
<input id="down9" type="button" name="down" onClick="test2('down9');"value="下移"></td>
</tr>
</table>
</form>
</body>
</html>
这篇关于JS实现上移下移功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!