请高手帮忙啊!小妹谢过了!我想在拖动时将拖动目标复制到另一个表格,然后从另一个表格拖回,删除当前重复存在的!!谢谢大家了!!
var draged = false;
tdiv = null;
var newTarget =null;
var dragCopyName =null;
var dragRemoveName =null;
var flag = false;
function mouseDown(){
ao = event.srcElement;
if((ao.tagName == "TD")||(ao.tagName == "TR"))ao = ao.offsetParent;
else return;
draged = true;
tdiv = document.createElement("div");
tdiv.innerHTML = ao.outerHTML;
tdiv.style.display = "block";//展开 none收拢
tdiv.style.position = "absolute";//绝对位置,配合上级框架的设定对本级框架设定,设置top、left值
tdiv.style.filter = "alpha(opacity=70)";//Blur(Add=4, Direction=8, Strength=4);滤镜
tdiv.style.cursor = "move";//e-resize;鼠标样式
tdiv.style.width = ao.offsetWidth;
tdiv.style.height = ao.offsetHeight;
tdiv.style.top = getInfo(ao).top;
tdiv.style.left = getInfo(ao).left;
document.body.appendChild(tdiv);
lastX = event.clientX;
lastY = event.clientY;
lastLeft = tdiv.style.left;
lastTop = tdiv.style.top;
newTarget = ao.cloneNode(true);
try{
ao.dragDrop();
}catch(e){}
//消除标题头
if(title.children.length>1){
for(var i=0;i<title.children.length;i++){
if(title.children(i).tagName=='B'){
title.removeChild(title.children(i));
}
}
}
flag=false;
}
//在整个从drag动作开始,直道drop动作结束都会触发的一个事件**判断MOUSE的位置**
function onMouseCopy(){
if(!draged)return;
var count =0;
var xPos = event.clientX;
var yPos = event.clientY;
tdiv.style.left = parseInt(lastLeft) + xPos - lastX;
tdiv.style.top = parseInt(lastTop) + yPos - lastY;
for(var i = 0;i < parentTable.cells.length;i++){
var parentCell = getInfo(parentTable.cells[i]);
if(xPos >= parentCell.left && xPos <= parentCell.right && yPos >= parentCell.top && yPos <= parentCell.bottom){
var subTables = parentTable.cells[i].getElementsByTagName("table");
if(subTables.length == 0 ){
if(xPos >= parentCell.left && yPos >= parentCell.top && xPos <= parentCell.right && yPos <= parentCell.bottom){
parentTable.cells[i].appendChild(newTarget);
dragCopyName=newTarget.innerText;
}
break;
}
for(var j = 0;j < subTables.length;j++){
var subTable = getInfo(subTables[j]);
if(xPos >= subTable.left && yPos >= subTable.top && !subTable.contains(newTarget) && xPos <= subTable.right && yPos <= subTable.bottom){
parentTable.cells[i].insertBefore(newTarget,subTables[j]);
dragCopyName=newTarget.innerText;
}
else{
parentTable.cells[i].appendChild(newTarget);
dragCopyName=newTarget.innerText;
}
}
}
}
}
//隐藏文本框获得拖动的文本节点
function dragNameCopy(){
if(dragCopyName!=null && dragCopyName!=''){
var name=document.getElementById('dragNameCopy');
name.value=name.value+dragCopyName+',';
dragCopyName='';
}
}
////在drop动作结束的时候,在来源对象上出发的一个事件。
function dragEnd(){
dragNameCopy();
if(!draged)return;
draged = false;
mm = ff(150,15);
}
//取得坐标
function getInfo(pos){
var object = new Object();
object.left = object.right = object.top = object.bottom = 0;
var tWidth = pos.offsetWidth;
var tHeight = pos.offsetHeight;
while(pos != document.body){
object.left += pos.offsetLeft;
object.top += pos.offsetTop;
pos = pos.offsetParent;
}
object.right = object.left + tWidth;
object.bottom = object.top + tHeight;
return object;
}
//用于恢复位置
function ff(aa,ab){
var ac = parseInt(getInfo(tdiv).left);
var ad = parseInt(getInfo(tdiv).top);
var ae = (ac-getInfo(ao).left)/ab;
var af = (ad-getInfo(ao).top)/ab;
return setInterval(function()
{
if(ab < 1)
{
clearInterval(mm);
tdiv.removeNode(true);
ao = null;
return
}
ab--;
ac-=ae;
ad-=af;
tdiv.style.left = parseInt(ac) + "px";
tdiv.style.top = parseInt(ad) + "px"
}
,aa/ab)
}
//初始化
function inint(){
for(var i = 0;i < parentTable.cells.length;i++){
var subTables = parentTable.cells[i].getElementsByTagName("table");
for(var j = 0;j < subTables.length;j++){
if(subTables[j].className != "dragTable")break;
flag=false;
subTables[j].rows[0].className="dragTable";
subTables[j].rows[0].attachEvent("onmousedown",mouseDown);
subTables[j].attachEvent("ondrag",onMouseCopy);
subTables[j].attachEvent("ondragend",dragEnd);
}
}
}
var draged = false;
tdiv = null;
var newTarget =null;
var dragCopyName =null;
var dragRemoveName =null;
var flag = false;
function mouseDown(){
ao = event.srcElement;
if((ao.tagName == "TD")||(ao.tagName == "TR"))ao = ao.offsetParent;
else return;
draged = true;
tdiv = document.createElement("div");
tdiv.innerHTML = ao.outerHTML;
tdiv.style.display = "block";//展开 none收拢
tdiv.style.position = "absolute";//绝对位置,配合上级框架的设定对本级框架设定,设置top、left值
tdiv.style.filter = "alpha(opacity=70)";//Blur(Add=4, Direction=8, Strength=4);滤镜
tdiv.style.cursor = "move";//e-resize;鼠标样式
tdiv.style.width = ao.offsetWidth;
tdiv.style.height = ao.offsetHeight;
tdiv.style.top = getInfo(ao).top;
tdiv.style.left = getInfo(ao).left;
document.body.appendChild(tdiv);
lastX = event.clientX;
lastY = event.clientY;
lastLeft = tdiv.style.left;
lastTop = tdiv.style.top;
newTarget = ao.cloneNode(true);
try{
ao.dragDrop();
}catch(e){}
//消除标题头
if(title.children.length>1){
for(var i=0;i<title.children.length;i++){
if(title.children(i).tagName=='B'){
title.removeChild(title.children(i));
}
}
}
flag=false;
}
//在整个从drag动作开始,直道drop动作结束都会触发的一个事件**判断MOUSE的位置**
function onMouseCopy(){
if(!draged)return;
var count =0;
var xPos = event.clientX;
var yPos = event.clientY;
tdiv.style.left = parseInt(lastLeft) + xPos - lastX;
tdiv.style.top = parseInt(lastTop) + yPos - lastY;
for(var i = 0;i < parentTable.cells.length;i++){
var parentCell = getInfo(parentTable.cells[i]);
if(xPos >= parentCell.left && xPos <= parentCell.right && yPos >= parentCell.top && yPos <= parentCell.bottom){
var subTables = parentTable.cells[i].getElementsByTagName("table");
if(subTables.length == 0 ){
if(xPos >= parentCell.left && yPos >= parentCell.top && xPos <= parentCell.right && yPos <= parentCell.bottom){
parentTable.cells[i].appendChild(newTarget);
dragCopyName=newTarget.innerText;
}
break;
}
for(var j = 0;j < subTables.length;j++){
var subTable = getInfo(subTables[j]);
if(xPos >= subTable.left && yPos >= subTable.top && !subTable.contains(newTarget) && xPos <= subTable.right && yPos <= subTable.bottom){
parentTable.cells[i].insertBefore(newTarget,subTables[j]);
dragCopyName=newTarget.innerText;
}
else{
parentTable.cells[i].appendChild(newTarget);
dragCopyName=newTarget.innerText;
}
}
}
}
}
//隐藏文本框获得拖动的文本节点
function dragNameCopy(){
if(dragCopyName!=null && dragCopyName!=''){
var name=document.getElementById('dragNameCopy');
name.value=name.value+dragCopyName+',';
dragCopyName='';
}
}
////在drop动作结束的时候,在来源对象上出发的一个事件。
function dragEnd(){
dragNameCopy();
if(!draged)return;
draged = false;
mm = ff(150,15);
}
//取得坐标
function getInfo(pos){
var object = new Object();
object.left = object.right = object.top = object.bottom = 0;
var tWidth = pos.offsetWidth;
var tHeight = pos.offsetHeight;
while(pos != document.body){
object.left += pos.offsetLeft;
object.top += pos.offsetTop;
pos = pos.offsetParent;
}
object.right = object.left + tWidth;
object.bottom = object.top + tHeight;
return object;
}
//用于恢复位置
function ff(aa,ab){
var ac = parseInt(getInfo(tdiv).left);
var ad = parseInt(getInfo(tdiv).top);
var ae = (ac-getInfo(ao).left)/ab;
var af = (ad-getInfo(ao).top)/ab;
return setInterval(function()
{
if(ab < 1)
{
clearInterval(mm);
tdiv.removeNode(true);
ao = null;
return
}
ab--;
ac-=ae;
ad-=af;
tdiv.style.left = parseInt(ac) + "px";
tdiv.style.top = parseInt(ad) + "px"
}
,aa/ab)
}
//初始化
function inint(){
for(var i = 0;i < parentTable.cells.length;i++){
var subTables = parentTable.cells[i].getElementsByTagName("table");
for(var j = 0;j < subTables.length;j++){
if(subTables[j].className != "dragTable")break;
flag=false;
subTables[j].rows[0].className="dragTable";
subTables[j].rows[0].attachEvent("onmousedown",mouseDown);
subTables[j].attachEvent("ondrag",onMouseCopy);
subTables[j].attachEvent("ondragend",dragEnd);
}
}
}
// JScript 文件Number.prototype.NaN0=function(){return isNaN(this)?0:this;} //全局变量
var iMouseDown=false;
var dragObject=null; //获得鼠标的偏移量(对象2-对象1)
function getMouseOffset(target,ev)
{
ev=ev||window.event;
var docPos=getPosition(target);
var mousePos=mouseCoords(ev);
return {x:mousePos.x-docPos.x,y:mousePos.y-docPos.y};
} //获得事件发生的实际位置----------------------对象1
function getPosition(e)
{
var left=0;
var top=0;
//相对位置累加得到实际位置
while(e.offsetParent)
{
left+=e.offsetLeft+(e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
top+=e.offsetTop+(e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
e=e.offsetParent;
}
left+=e.offsetLeft+(e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
top+=e.offsetTop+(e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
return {x:left,y:top};
}
//获得发生事件鼠标的位置----------------------对象2
function mouseCoords(ev)
{
if(ev.pageX||ev.pageY)
{
return {x:ev.pageX,y:ev.pageY};
}
return {x:ev.clientX+document.body.scrollLeft-document.body.clientLeft,y:ev.clientY+document.body.scrollTop-document.body.clientTop};
}
//定义可以拖拽的对象
function makeDragable(item)
{
if(!item) return;
//为可拖拽对象定义一个onmousedown事件的方法
ev=window.event;
item.onmousedown=function(ev)
{
dragObject=this;
mouseOffset=getMouseOffset(this,ev);
return false;
}
} //定义鼠标点下所调用的方法
function mouseDown(ev)
{
ev=ev||window.event;
var target=ev.target||ev.srcElement;
if(target.onmousedown||target.getAttribute('DragObj'))
{
return false;
}
}
//鼠标抬起后释放对象
function mouseUp(ev)
{
dragObject = null;
//onmouseup事件触发时说明鼠标已经松开,所以设置down变量值为false
iMouseDown = false;
} //鼠标移动
function mouseMove(ev)
{
ev=ev||window.event;
var target = ev.target || ev.srcElement;
var mousePos = mouseCoords(ev);
if(dragObject)
{
if(dragObject.style)
{
//移动对象
dragObject.style.left=mousePos.x - mouseOffset.x;
dragObject.style.top= mousePos.y - mouseOffset.y;
}
}
//lMouseState = iMouseDown;
if(dragObject) return false;
}
document.onmousedown=mouseDown;
document.onmousemove=mouseMove;
document.onmouseup=mouseUp;
// function movedivEdit( )
// {
// var element=document.getElementById('divEdit');
// makeDragable(element);
// }