document.elementFromPoint在IE上返回null [英] document.elementFromPoint returning null on IE

查看:113
本文介绍了document.elementFromPoint在IE上返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the document.elementFromPoint for getting the selected element for dragging it. But it returns null. When i debug the same and then re run the same code, object is returned.

以下是代码:

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;
}


推荐答案

看起来我们有同样讨厌的想法,哈哈。但我正在研究物体碰撞。看,你在哪里调用GetZoneFromPoint。光标点击事件仅在浏览器中调用,对吧?所以,不需要先检查x或y,只需检查zone = null。你仔细检查elementFromPoint是什么时候x&不变?我在这里完成了工作流程。

it's look like we have the same nasty idea, haha. But i'm working on object collision. Look, where you call GetZoneFromPoint. Cursor click event only invoked in browser, right? So, no need to check x or y, just zone=null at first. You double check elementFromPoint for what while x & y unchanged? I'm done work flow here.

一些提示:
- 我有2个'绝对'DIV对象,称为A& B在右下角与B重叠。
- 使用如下:
var X = parseInt(B.offsetLeft);
var Y = parseInt(B.offsetTop);
alert(document.elementFromPoint(X,Y).innerHTML);
- 为什么要解析? offsetLeft以格式返回:number +px
- 结果是:IE->返回A而FF-> B
- 如果是X-1或Y-1,则两个浏览器都返回A.
- 最后的话:不要单独使用document.body.clientWidth检查解析,试试这个document.body.clientWidth-o.clientWidth。希望这些能给你一个加强奖励。干得好!

some tips: - I have 2 'absolute' DIV objects called A & B which B is overlap A at bottom right corner. - Use like this: var X=parseInt(B.offsetLeft); var Y=parseInt(B.offsetTop); alert(document.elementFromPoint(X,Y).innerHTML); - Why parse? offsetLeft return in format: number+"px" - The result is: IE->return A while FF->B - If X-1 or Y-1, both browsers return A. - Final words: don't use document.body.clientWidth alone check resolution, try this document.body.clientWidth-o.clientWidth. Hope these give you a power up bonus. Cheer!

这篇关于document.elementFromPoint在IE上返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆