需要找到用户单击的元素的边界

Need to find the boundary of the element on which the user clicks

本文关键字:元素 边界 单击 用户      更新时间:2023-09-26

在我的Firefox扩展中,我需要获得用户单击的元素边界。

我可以使用document.elementFromPoint()来获取元素。但是我想知道边界离点击点有多远。

任何想法?

您可以使用getClientRects。它将返回一个ClientRectList,它是ClientRect对象的一个类似数组的对象。这些ClientRect对象具有bottomtopleftright等属性。

var rectList = element.getClientRects();
var rect = rectList[0];
alert(rect.left);