Code to find absolute position of an element of DO

2019-06-01 08:07发布

问题:

I am working on a project in Java.

In this project, I have to find absolute position of an element of DOM. But I don't know how to do this.

I searched on net, I found the same for Javascript. I found this from here.

Code is this,

function getPosition(element) {
    var xPosition = 0;
    var yPosition = 0;

    while(element) {
        xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft);
        yPosition += (element.offsetTop - element.scrollTop + element.clientTop);
        element = element.offsetParent;
    }
    return { x: xPosition, y: yPosition };
} 

When I try to write this code in Java, offsetLeft, offsetTop variable is not found. Can you tell me, how can I write this code in Java?

Edit No. 1

Is there any method using Jsoup for the same?

回答1:

There are only two methods giving you a position in Jsoup:

  • Element.elementSiblingIndex()
  • Node.siblingIndex()

(you can get the count of childs too)

But there's no offsetLeft or offsetTop in Jsoup