如何检测元素的绝对位置(顶部,左侧)(也适用于IE)? [英] How to detect the absolute position (top, left) of an element (also for IE) ?

查看:89
本文介绍了如何检测元素的绝对位置(顶部,左侧)(也适用于IE)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AJAX在网页上动态创建元素。我添加了活动

(比如onclick)。一切正常我可以回溯

事件的起源,父节点,但是现在我必须在

上放一个图层(一个表格)来自事件起源于哪个。我已经在

网站上找到了一些适用于FF和Opera的代码,但不适用于IE(6& 7)。

的顶部位置是好的,但不是左边(对于IE来说,在

左边超过100px)。我找到了另一种方法来获得元素的绝对x位置
,但仅适用于IE 6& 7.如何在浏览器之间区分

?通常你会测试一个对象是否存在,就好像

(document.all)...问题:Opera也知道如何处理

document.all。目前我用这个:


函数getXYCoordinates(obj){

var curleft = curtop = 0;

var x = obj.offsetLeft; //对于IE

if(obj.offsetParent){

curleft = obj.offsetLeft;

curtop = obj.offsetTop;

while(obj = obj.offsetParent){

curleft + = obj.offsetLeft;

curtop + = obj.offsetTop;

}

}

如果(document.all){//这里我应该测试IE,但Opera也会获得

抓住

返回[x,curtop];

}否则{

返回[curleft,curtop];

} < br $>
}


适用于FF和IE 6& 7但不适用于Opera。如果我跳过测试

(document.all)并返回[curleft,curtop]它适用于FF和

Opera但不适用于IE 6& 7.

如何测试相关对象以确定我应该返回

[x,curtop]而不是[curleft,curtop]?

或者有更好的方法来确定一个物体的位置

引发一个事件。


thanx,


Pugi!

解决方案



Pugi!写道:


}

if(document.all){//这里我应该测试IE,但是Opera得到

抓住

返回[x,curtop];

}否则{

返回[curleft,curtop];

}

}



if(document.all&& user_agent.indexOf(" opera")= = -1)

{

// IE

}

其他

{

// FF,歌剧

}


marss在12/19上说了以下内容/ 2006 5:05 AM:


Pugi!写道:


> }
if(document.all){//这里我应该测试IE,但是Opera也会被抓住
返回[x,curtop];
}其他{}
}



if(document.all&& user_agent.indexOf(" opera")== -1)

{

// IE



不,它是一个浏览器支持document.all并且没有使用userAgent字符串的
中的opera。它确实*不*意味着它是IE。


-

兰迪

机会有利于准备好的心灵

comp.lang.javascript常见问题 - http://jibbering.com/faq

Javascript最佳实践 - http://www.JavascriptToolbox.com / bestpractices /




var user_agent = navigator.userAgent.toLowerCase();

if (document.all&& user_agent.indexOf(" opera")== -1)

{

// IE

}

其他

{

// FF,歌剧

}


I create elements dynamically on a webpage using AJAX. I add events
(like onclick). Everything works fine I can retrace the origin of the
event, the parentnode, ... but now I have to put a layer (a form) on
the image from which the event originated. I have foun some code on the
web that works fine for FF and Opera, but not for IE (6 & 7). The
top-position is good, but not the left (more then 100px to the left of
for IE). I have found another method that gets the absolute x-position
of an element but works only good for IE 6 & 7. How do I distinguish
between browsers ? Normally you test wether an object exists like if
(document.all) ... Problem: Opera also knows how to handle
document.all. For the moment I use this:

function getXYCoordinates(obj) {
var curleft = curtop = 0;
var x = obj.offsetLeft; // for IE
if (obj.offsetParent) {
curleft = obj.offsetLeft;
curtop = obj.offsetTop;
while (obj = obj.offsetParent) {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
}
}
if (document.all) { // here I should test for IE, but also Opera gets
caught
return [x,curtop];
} else {
return [curleft,curtop];
}
}

This works for FF and IE 6 & 7 but not for Opera. If I skip the test
(document.all) and return [curleft, curtop] it works fine for FF and
Opera but not for IE 6 & 7.
How can I test an relevant object to determine wether I should return
[x, curtop] instead of [curleft, curtop] ?
Or is there a better method to determine the position of an object that
raised an event.

thanx,

Pugi!

解决方案


Pugi! wrote:

}
if (document.all) { // here I should test for IE, but also Opera gets
caught
return [x,curtop];
} else {
return [curleft,curtop];
}
}

if (document.all && user_agent.indexOf("opera") == -1)
{
//IE
}
else
{
//FF,Opera
}


marss said the following on 12/19/2006 5:05 AM:

Pugi! wrote:

> }
if (document.all) { // here I should test for IE, but also Opera gets
caught
return [x,curtop];
} else {
return [curleft,curtop];
}
}


if (document.all && user_agent.indexOf("opera") == -1)
{
//IE

No, it is a browser that supports document.all and doesn''t have opera in
the userAgent string. It does *NOT* mean it is IE.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/



var user_agent = navigator.userAgent.toLowerCase();
if (document.all && user_agent.indexOf("opera") == -1)
{
//IE
}
else
{
//FF,Opera
}


这篇关于如何检测元素的绝对位置(顶部,左侧)(也适用于IE)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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