window.onload = function(){ //绑定事件 var addEvent = document.addEventListener ? function(el,type,callback){ el.addEventListener( type, callback, !1 ); } : function(el,type,callback){ el.attachEvent( "on" + type, callback ); } //移除事件 var removeEvent = document.removeEventListener ? function(el,type,callback){ el.removeEventListener( type, callback ); } : function(el,type,callback){ el.detachEvent( "on" + type, callback); } //精确获取样式 var getStyle = document.defaultView ? function(el,style){ return document.defaultView.getComputedStyle(el, null).getPropertyValue(style) } : function(el,style){ style = style.replace(/\-(\w)/g, function($, $1){ return $1.toUpperCase(); }); return el.currentStyle[style]; } var dragManager = { clientY:0, draging:function(e){//mousemove时拖动行 var dragObj = dragManager.dragObj; if(dragObj){ e = e || event;//清除选区 if(window.getSelection){//w3c window.getSelection().removeAllRanges(); }else if(document.selection){ document.selection.empty();//IE } var y = e.clientY; var down = y > dragManager.clientY;//是否向下移动 var tr = document.elementFromPoint(e.clientX,e.clientY); if(tr && tr.nodeName == "TD"){ tr = tr.parentNode dragManager.clientY = y; if( dragObj !== tr){ tr.parentNode.insertBefore(dragObj, (down ? tr.nextSibling : tr)); } }; } }, dragStart:function(e){ e = e || event; var target = e.target || e.srcElement; if(target.nodeName === "TD"){ target = target.parentNode; dragManager.dragObj = target; if(!target.getAttribute("data-background")){ var background = getStyle(target,"background-color"); target.setAttribute("data-background",background) } //显示为可移动的状态 target.style.backgroundColor = "#ccc"; target.style.cursor = "move"; dragManager.clientY = e.clientY; addEvent(document,"mousemove",dragManager.draging); addEvent(document,"mouseup",dragManager.dragEnd); } }, dragEnd:function(e){ var dragObj = dragManager.dragObj if (dragObj) { e = e || event; var target = e.target || e.srcElement; if(target.nodeName === "TD"){ target = target.parentNode; dragObj.style.backgroundColor = dragObj.getAttribute("data-background"); dragObj.style.cursor = "default"; dragManager.dragObj = null; removeEvent(document,"mousemove",dragManager.draging); removeEvent(document,"mouseup",dragManager.dragEnd); } } }, main:function(el){ addEvent(el,"mousedown",dragManager.dragStart); } } var el = document.getElementById("table"); dragManager.main(el); }
实现代码二
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" > <head > <title>无标题页</title> </head> <body> <table cellpadding="3"> <tr> <th>移动</th> <th>数据</th> <th>数据</th> <th>数据</th> </tr> <tr> <td>*</td> <td>11111111111</td> <td>sdgergserhserhserh</td> <td>awegaw</td> </tr> <tr> <td>*</td> <td>222222222</td> <td>serherwwwwww</td> <td>fafafff</td> </tr> <tr> <td>*</td> <td>333333333</td> <td>qqqqqqqwewer</td> <td>yukyuk</td> </tr> </table> <script type="text/javascript"> var curTr = null; var tb1 = document.getElementById('tb1'); var trs = tb1.getElementsByTagName('tr'); tb1.onselectstart = function(){ if(curTr){ document.selection.empty(); return true; } }; for(var i=1; i<trs.length; i++) { var tds = trs[i].getElementsByTagName('td'); tds[0].style.cursor = 'move'; tds[0].onmousedown = function(){ curTr = this.parentNode; curTr.style.backgroundColor = '#eff'; }; tds[0].onmouseover = function() { if(curTr && curTr != this.parentNode) { this.parentNode.swapNode(curTr); } } } document.body.onmouseup = function(){ if(curTr){ curTr.style.backgroundColor = ''; curTr = null; } }; </script> </body> </html>
注:不兼容firefox,在IE和chrome下测试通过哦。