Code to find absolute position of an element of DOM in Java

霸气de小男生 提交于 2019-12-08 10:00:56

问题


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



来源:https://stackoverflow.com/questions/15740533/code-to-find-absolute-position-of-an-element-of-dom-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!