一个超级郁闷的问题...拖动实现了, 在IE下其子标签的事件却被屏蔽了

btbtd 2008-05-29 10:40:24
下载附件
http://shawlqiu.googlegroups.com/web/DragSort.7z?gsc=J-bTVQsAAAAYUeHnV1iagupL4GVnAh1d

就是拖动域里的链接啥的都点击不到...




/*
shawl.qiu code
class: DragSort
version: 1.0
date: 2008-x-x

updated: ver, name, date, summary.
updated x: ver, name, date, summary.
*/

var GCount = 1;

if(typeof __DragSort=="undefined")
{
__DragSort = {};
__DragSort.Set = {};
__DragSort.SourceEle = undefined;
__DragSort.TempEle = undefined;
__DragSort.DspDragEle = undefined;
__DragSort.GIndex = 999;
__DragSort.Pos = {x:0, y:0};
}

if(typeof HTMLElement!="undefined")
{
Event.prototype.__defineSetter__
(
"returnValue"
, function (bBool)
{
if(bBool) this.stopPropagation();
else this.preventDefault();
}
);

Event.prototype.__defineGetter__
(
"srcElement"
, function (bBool)
{
return this.target;
}
);

HTMLElement.prototype.attachEvent =
function(sEvtName, Func, bCaptures)
{
if(!bCaptures) bCaptures = false;
sEvtName = sEvtName.slice(2);
this.addEventListener(sEvtName, Func, bCaptures);
};
document.attachEvent = HTMLElement.prototype.attachEvent;

HTMLElement.prototype.detachEvent =
function(evt, fn, bCaptures)
{
evt = evt.slice(2);
this.removeEventListener(evt, fn, bCaptures);
};
document.detachEvent = HTMLElement.prototype.detachEvent;
}/* if(typeof HTMLElement!="undefined") */

function DragSort()
{
var This = this;

This.Set = [];
}

DragSort.prototype.Main = DragSort_Main;

function DragSort_Main()
{
var This = this;

for(var i=0, j=This.Set.length; i<j; i++)
{
var Item = This.Set[i];
Item.This = This;

//Item.attachEvent("onmousedown", DragSort_Mousedown);
Item.onmousedown = DragSort_Mousedown;
//Item.onmouseup = DragSort_Mouseup;
}
document.attachEvent("onmouseup", DragSort_Mouseup);

}/* function DragSort_Main() */

function DragSort_Mousedown(e)
{
e = e||window.event;
e.returnValue = true;

var TempEle = e.srcElement, XEle;

while(TempEle = TempEle.parentNode)
{
if(!TempEle.tagName) break;
if(TempEle.getAttribute("IsDrag")!=null)
{
XEle = TempEle;
}
}
if(!XEle) return true;

document.attachEvent("onmousemove", DragSort_Mousemove);

fSetCapture(e, XEle);

if(XEle.id in __DragSort.Set==false)
{
__DragSort.Set[XEle.id] = XEle;
}
__DragSort.SourceEle = XEle;

__DragSort.DspDragEle = XEle.cloneNode(true);
__DragSort.DspDragEle.id = "";
__DragSort.DspDragEle.This = XEle.This;

var Pos = fGetXYWH(__DragSort.SourceEle);

with(__DragSort.DspDragEle.style)
{
position = "absolute";
//top = "-100px";
zIndex = ++__DragSort.GIndex;
}
XEle.parentNode.appendChild(__DragSort.DspDragEle);

fAddClass(__DragSort.DspDragEle, "DragDspClass");
fAddClass(__DragSort.SourceEle, "DragDownClass");

__DragSort.Pos.y = e.clientY - parseInt(Pos.Top);

}

function DragSort_Mousemove(e)
{
e = e||window.event;
e.returnValue = true;

var TempEle = e.srcElement, XEle;

while(TempEle.tagName)
{
if(TempEle.getAttribute("IsDrag")!=null)
{
XEle = TempEle;
break;
}
TempEle = TempEle.parentNode
}
if(!XEle) return;
if(!__DragSort.DspDragEle) return;

var Left = e.clientX - __DragSort.Pos.x;
var Top = e.clientY - __DragSort.Pos.y;
__DragSort.DspDragEle.style.top = Top + "px";

var Pos1 = fGetXYWH(XEle);
var Pos2 = fGetXYWH(__DragSort.SourceEle);

var bBefore= true;
if(Pos2.Top<Pos1.Top) bBefore = false;
}

function DragSort_Mouseup(e)
{
e = e||window.event;
e.returnValue = true;

document.detachEvent("onmousemove", DragSort_Mousemove);

var ex = document.documentElement.scrollLeft+e.clientX;
var ey = document.documentElement.scrollTop+e.clientY;

var Pos1 = Pos2 = null, TempEle = e.srcElement, XEle;

while(TempEle.tagName)
{
if(TempEle.getAttribute("IsDrag")!=null)
{
XEle = TempEle;
break;
}
TempEle = TempEle.parentNode
}
if(!XEle) return;
var This = XEle.This;
if(__DragSort.DspDragEle)
{
try
{

}
catch(ex){}
__DragSort.DspDragEle.parentNode.removeChild(__DragSort.DspDragEle);

}
else
{
return;
}
fRemoveClass(__DragSort.SourceEle, "DragDownClass");

for(var i=0, j=This.Set.length; i<j; i++)
{
var Item = This.Set[i];
var Pos = fGetXYWH(Item);

var MaxTop = Pos.Top+Pos.Height;
if(Pos.Top<=ey&&MaxTop>=ey)
{
GetEle = Item;
break;
}
}

if(!GetEle) return;
Pos1 = fGetXYWH(__DragSort.SourceEle);
Pos2 = fGetXYWH(GetEle);


var bBefore= true;
if(Pos2.Top>Pos1.Top) bBefore = false;
fInsertNode(GetEle, __DragSort.SourceEle, bBefore);

__DragSort.Set = {};
__DragSort.SourceEle = undefined;
__DragSort.TempEle = undefined;
__DragSort.DspDragEle = undefined;
__DragSort.GIndex = 999;
__DragSort.Pos = {x:0, y:0};
}

function fSetCapture(EvtForFf, EleForIe)
{
if(typeof HTMLElement !="undefined")
{
EvtForFf.stopPropagation();
EvtForFf.preventDefault();
}
else try{EleForIe.setCapture()}catch(ex){};
}/* function fSetCapture(EvtForFf, EleForIe) */

function fReleaseCapture(EvtForFf, EleForIe)
{
if(typeof HTMLElement !="undefined")EvtForFf.stopPropagation();
else EleForIe.releaseCapture();
}/* function fReleaseCapture(EvtForFf, EleForIe) */

function fGetXYWH(Ele)
{/* shawl.qiu code, return object */
var a = new Array();
var t = Ele.offsetTop, l = Ele.offsetLeft, w = Ele.offsetWidth, h = Ele.offsetHeight;
while(Ele = Ele.offsetParent){ t += Ele.offsetTop; l += Ele.offsetLeft; }
return {Top:t, Left:l, Width:w, Height:h};
}/* end function fGetXYWH(Ele) */

function fInsertNode(OldNode, NewNode, bBefore)
{/* shawl.qiu script, return default */
var ParentNode = OldNode.parentNode;
if(!ParentNode){document.body.appendChild(NewNode); return false; };
if(bBefore){ ParentNode.insertBefore(NewNode,OldNode); return false; }
ParentNode.replaceChild(NewNode, OldNode);
return ParentNode.insertBefore(OldNode, NewNode);
}/* end function fInsertNode(OldNode, NewNode, bBefore) */

function fSetOpacity(XEle, iAlpha)
{/* shawl.qiu code, return void */
XEle.style.opacity = iAlpha/100;
XEle.style.filter = 'alpha(opacity=' + iAlpha + ')';
return;
}/* function fSetOpacity(XEle, iAlpha) */

function fAddClass(XEle, XClass)
{/* shawl.qiu code, void return */
if(!XClass) throw new Error("XClass 不能为空!");
if(XEle.className!="")
{
var Re = new RegExp("\\b"+XClass+"\\b\\s*", "");
XEle.className = XEle.className.replace(Re, "");
XEle.className = XClass+" "+XEle.className;
}
else XEle.className = XClass;
}/* end function fAddClass(XEle, XClass) */

function fRemoveClass(XEle, XClass)
{/* shawl.qiu code, void return */
if(!XClass) throw new Error("XClass 不能为空!");
if(XEle.className!="")
{
var Re = new RegExp("\\b"+XClass+"\\b\\s*", "");
XEle.className = XEle.className.replace(Re, "");
}
}/* function fRemoveClass(XEle, XClass) */


...全文
277 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 btbtd 的回复:]
if(tag.tagName!="TD"&&tag.tagName!="TR")return;//===================
这是硬编码...

我做的东西要求是通用的..
所以用了个 Callback ,,,,
[/Quote]
这个也不能说是硬编码
  • 打赏
  • 举报
回复
呵呵,有了思路,那些都是小问题了!注意下细节就没什么了
yellowlovenet 2008-05-30
  • 打赏
  • 举报
回复
看不懂
btbtd 2008-05-30
  • 打赏
  • 举报
回复
到时全部以Callback 进行判断...
不同应用只要写各个不同的 简单的..Callback 就可以使用了..
btbtd 2008-05-30
  • 打赏
  • 举报
回复
呵呵 ..
我那不是还没写完吧..
写完后无论什么标签...只要父标签有 IsDrag 属性...
就会自己判断...
  • 打赏
  • 举报
回复
都是牛牛啊。。
shuai123456 2008-05-30
  • 打赏
  • 举报
回复
太长了把
Go 旅城通票 2008-05-30
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 btbtd 的回复:]
if(tag.tagName!="TD"&&tag.tagName!="TR")return;//===================
这是硬编码...

我做的东西要求是通用的..
所以用了个 Callback ,,,,
[/Quote]

不算硬编码了吧....你这个也只能用到表格的行上面,如果是使用div的流格式形成表格样式,你的代码也运行不了啊


要不你可以给不需要拖拽的标签加个属性IsDrag="false",然后使用下面的代码嘎

if(tag.getAttribute("IsDrag")=="false")return;//=================== 
  • 打赏
  • 举报
回复
呵呵,看来showbo JS用的很老练了

一下子想到了这个,不是经你的提醒说有这么一个函数我还想不起来可以做这么一个判断
btbtd 2008-05-30
  • 打赏
  • 举报
回复
if(typeof This.Callback.MousedownFilter=="function") if(!This.Callback.MousedownFilter(SourceEle, XEle, This)) return false;
btbtd 2008-05-30
  • 打赏
  • 举报
回复
if(tag.tagName!="TD"&&tag.tagName!="TR")return;//===================
这是硬编码...

我做的东西要求是通用的..
所以用了个 Callback ,,,,

  • 打赏
  • 举报
回复
呵呵,我倒是没看到他里面有DragSort_Mousedown这么一个Mousedown的动作函数
做这个判断的省了好多事

  • 打赏
  • 举报
回复
恩,加上这个也行
Go 旅城通票 2008-05-30
  • 打赏
  • 举报
回复
在mousedown时多加个判断就好了,如果不是td或者tr,就返回,不执行拖拽



function DragSort_Mousedown(e)
{
e = e||window.event;
e.returnValue = true;
var tag=e.srcElement;
if(tag.tagName!="TD"&&tag.tagName!="TR")return;//===================
//其他代码




  • 打赏
  • 举报
回复
-_-!!
YH_Random 2008-05-30
  • 打赏
  • 举报
回复
之前不是我说的,另一个人说的。。。。
btbtd 2008-05-30
  • 打赏
  • 举报
回复
...
楼上眼神有待思考....
YH_Random 2008-05-30
  • 打赏
  • 举报
回复
←又见传说中的"李宇春",飘过,too
  • 打赏
  • 举报
回复
你里面不有几个地方用到getAttribute这个的
btbtd 2008-05-30
  • 打赏
  • 举报
回复
:)
还有不同?
这倒是没仔细看过...
加载更多回复(17)

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧