纯Javascript中的元素坐标
问题描述:
假设我在div(或任何其他包含元素,或者可能只是在文档正文中)中有一个元素。如何获得该元素相对于其容器的(x,y)坐标?
Say that I have an element inside a div (or any other containing element, or perhaps just in the body of the document). How do I get the (x,y) coordinates of that element, relative to its container?
我需要能够在纯Javascript中完成...
And I need to be able to do it in pure Javascript...
答
使用以下
document.getElementById("elementId").offsetTop;
document.getElementById("elementId").offsetLeft;