2010-12-01 79 views
2

我正在使用document.elementFromPoint获取所选元素以进行拖动。但它返回null。当我调试相同,然后重新运行相同的代码,对象返回。document.elementFromPoint在IE上返回null

以下是代码:

function GetZoneFromPoint(x, y, prtDrag, elemIBeam,evnt) { 
    if (x == null || y == null || prtDrag == null || elemIBeam == null) 
     alert("Null in GetZone"); 
    var prtDragZIndexOld = prtDrag.style.zIndex; 
    var elemIBeamZIndexOld = elemIBeam.style.zIndex; 
    prtDrag.style.zIndex = -1; 
    elemIBeam.style.zIndex = -1; 
    var zone; 
    zone = document.elementFromPoint(x, y); 
    prtDrag.style.zIndex = prtDragZIndexOld; 
    elemIBeam.style.zIndex = elemIBeamZIndexOld; 
    if (zone == null) { 
     zone = document.elementFromPoint(x, y); 
     if (zone == null) { 
      debugger; 
      zone = event.rangeParent; 
     } 
    } 
    if (zone == null) { 
     alert('null'); 
     return null; 
    } 
    if (x < 0 || x > document.body.clientWidth || 
     y < 0 || y > document.body.clientHeight) { 
     zone = null; 
    } 
    else if ((zone.className == 'LayoutWellElement') || 
      (zone.className == 'LayoutMainElement') || 
      (zone.className == 'ElementFrame')) { 
     while ((!FIsZone(zone)) && (zone.tagName != 'BODY')) { 
      zone = zone.parentElement; 
     } 
    } 
    if (!FIsZone(zone)) { 
     zone = null; 
    } 
    return zone; 
} 

回答

0

它看起来像我们有相同的馊主意,哈哈。但我正在研究物体碰撞。看,你在哪里调用GetZoneFromPoint。光标单击事件只在浏览器中调用,对吗?所以,不需要检查x或y,刚开始zone = null。您在x & y保持不变的情况下再次检查elementFromPoint?我在这里完成工作流程。

一些提示: - 我有2个'绝对'DIV对象,名为A & B其中B在右下角重叠A. - 像这样使用: var X = parseInt(B.offsetLeft); var Y = parseInt(B.offsetTop); alert(document.elementFromPoint(X,Y).innerHTML); - 为什么解析? offsetLeft返回的格式为:number +“px” - 结果是:IE-> return A while FF-> B - 如果X-1或Y-1,则两个浏览器都返回A. - 最终字词:不要使用单独检查document.body.clientWidth检查解析,试试这个document.body.clientWidth-o.clientWidth。希望这些给你一个通电奖金。欢呼!

相关问题