Get position off all divs

This question already has an answer here:

  • Retrieve the position (X,Y) of an HTML element 23 answers

  • Try with this:

    var divs = window.frames[0].document.getElementsByTagName("div")
        i = 0, rects = [];
    for (; i < divs.length; i++)
        rects.push(divs[i].getBoundingClientRect());
    

    Now the array rects contains objects that define the position relative to the viewport of all your divs. Each of the elements will be something like this:

    {
        top: ...,
        right: ...,
        bottom: ...,
        left: ...,
        width: ...,
        height: ...
    }
    

    Those properties are the number of pixel from each border (or the width or height).

    IE6-8 don't report the width and height properties, though, but it's easy to compute them.


    getElementsByTagName returns an array. The array object has no x property.

    You will need to iterate over the members, collecting each value of x .

    Furthermore (as pointed out in comments below), divs don't have an x property. See the question "Retrieve the position (X,Y) of an HTML element" for details on how to manage that.

    链接地址: http://www.djcxy.com/p/15604.html

    上一篇: 元素目的地

    下一篇: 获取所有div的位置