在Java中查找DOM元素的绝对位置的代码 [英] Code to find absolute position of an element of DOM in Java
问题描述
我正在使用 Java 中的一个项目。
I am working on a project in Java.
在这个项目中,我必须找到DOM元素的绝对位置。但是我不知道该怎么做。
In this project, I have to find absolute position of an element of DOM. But I don't know how to do this.
我在网上搜索,我发现Javascript一样。我从此处找到此信息。
I searched on net, I found the same for Javascript. I found this from here.
代码是,
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 };
}
当我尝试用Java编写这段代码时, offsetLeft,offsetTop 找不到变量。你能告诉我,我该如何用Java编写这个代码?
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?
编辑1
有没有使用 Jsoup
Is there any method using Jsoup for the same?
推荐答案
只有两种方法可以让您在Jsoup中找到一个位置:
There are only two methods giving you a position in Jsoup:
- Element.elementSiblingIndex()
- Node.siblingIndex()
(你可以得到孩子们)
但是没有 offsetLeft
或 offsetTop
在Jsoup中
But there's no offsetLeft
or offsetTop
in Jsoup
这篇关于在Java中查找DOM元素的绝对位置的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!