如何查找HTML按钮或图像的坐标,跨浏览器? [英] how to find coordinates of an HTML button or image, cross browser?

查看:154
本文介绍了如何查找HTML按钮或图像的坐标,跨浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近在comp.lang.javascript中有一个线程,在那里发布了
的胜利,但没有发布代码:

There was a thread on this in comp.lang.javascript recently where victory was announced but no code was posted:

在HTML页面上如何您可以跨浏览器和页面样式可靠地找到元素的左下角坐标(图像或按钮,例如),
?在Ajax in Action(我的副本)中倡导的方法在某些情况下似乎不起作用。为了使问题更容易,我们假设我们可以将全局文档样式设置为传统或过渡或其他任何方式。

On an HTML page how do you find the lower left corner coordinates of an element (image or button, say) reliably across browsers and page styles? The method advocated in "Ajax in Action" (copy I have) doesn't seem to work in IE under some circumstances. To make the problem easier, let's assume we can set the global document style to be "traditional" or "transitional" or whatever.

请提供代码或指向代码的指针请(一个完整的功能,适用于所有浏览器) - 不要只说那很容易,并且嘲笑遍历DOM - 如果我想读这种东西我会回到comp.lang。 JavaScript的。请骂我,如果这是一个重复,并指出我的解决方案 - 我试图找到它。

Please provide code or a pointer to code please (a complete function that works on all browsers) -- don't just say "that's easy" and blather about what traversing the DOM -- if I want to read that kind of thing I'll go back to comp.lang.javascript. Please scold me if this is a repeat and point me to the solution -- I did try to find it.

推荐答案

在我的经验,唯一可靠的方式来获得这样的东西,正在使用 JQuery (不要害怕,它只是一个外部脚本文件,你必须包括)。然后,您可以使用

In my experience, the only sure-fire way to get stuff like this to work is using JQuery (don't be afraid, it's just an external script file you have to include). Then you can use a statement like

$('#element').position()

$('#element').offset()

获取当前的坐标,在任何和所有浏览器我到目前为止遇到过。

to get the current coordinates, which works excellently across any and all browsers I've encountered so far.

这篇关于如何查找HTML按钮或图像的坐标,跨浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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