首页 > Js/Jquery

Js表格行上移下移例子

发表于2014-09-22 16:01:35| --次阅读| 来源webkfa| 作者小猪仔

摘要:Js表格行上移下移例子
js代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>表格行上移下移例子</TITLE>
 </HEAD>
 <BODY>
	<div id="webkfa">
	</div>
	<script type="text/javascript">
		var arr=[
			{id:1,name:'web开发1'},
			{id:2,name:'webkfa2'},
			{id:3,name:'webkfa3'},
			{id:4,name:'webkfa4'},
			{id:5,name:'webkfa5'},
			{id:5,name:'webkfa6'},
			{id:5,name:'webkfa7'},
			{id:5,name:'webkfa8'}
		]
		function sctablehtml(arr){
			var html=[];
			html[html.length]='<table border="1">';
			html[html.length]='<tr>';
			html[html.length]='<td>序号</td>';
			html[html.length]='<td>名字</td>';
			html[html.length]='<td>操作</td>';
		    html[html.length]='</tr>';
			for(var i=0;i<arr.length;i++){
				var obj=arr[i];
				html[html.length]='<tr>';
				html[html.length]='<td>'+obj.id+'</td>';
				html[html.length]='<td id="tdname'+i+'">'+obj.name+'</td>';
				html[html.length]='<td>';
				if(i>0){
					html[html.length]='<input type="button" onclick="premove('+i+')" value="上移"/>';
				}
				if(i<arr.length-1){
					html[html.length]='<input type="button" onclick="nextmove('+i+')" value="下移"/>';
				}
				html[html.length]='</td>';
				html[html.length]='</tr>';
			}
			html[html.length]='</table>';
			document.getElementById("webkfa").innerHTML=html.join("");
		}

		function premove(index){
			var preindex=index-1;
			//交换本行和上一行的值
			var tmpstr=document.getElementById("tdname"+preindex).innerHTML;
			document.getElementById("tdname"+preindex).innerHTML=document.getElementById("tdname"+index).innerHTML
			document.getElementById("tdname"+index).innerHTML=tmpstr
		}
		function nextmove(index){
			var nextindex=index+1;
			//交换本行和上一行的值
			var tmpstr=document.getElementById("tdname"+nextindex).innerHTML;
			document.getElementById("tdname"+nextindex).innerHTML=document.getElementById("tdname"+index).innerHTML
			document.getElementById("tdname"+index).innerHTML=tmpstr
		}
		sctablehtml(arr);
	</script>
 </BODY>
</HTML>

相关文章

猜你喜欢

学到老在线代码浏览器 关闭浏览
友情链接: hao123 360导航 搜狗网址导航 114啦网址导航 博客大全
Copyright © 1999-2014, WEBKFA.COM, All Rights Reserved  京ICP备14034497号-1