获取句子开头和结尾的坐标
我有一个 ,其中包含一个句子。例如:
<span>A sentence residing within a span</span>
是否可以在附加到该跨度的 Javascript 事件(例如 onmouseover
)上分别获取该句子的开头和结尾字母(即“A”和“n”)的坐标?
该跨度位于 div
内。所以
case - I:对于句子
This is a sentence inline
The top and left 是根据单词“This”的字母“T”进行评估的。
case - II:对于句子
... ... ... ... ... ... This is
a sentence broke into two lines.
顶部根据单词“This”的字母“T”进行评估,左侧根据“a”进行评估。
有没有办法获取一个句子的两个端点的坐标?
I have a <span></span>
, within which a sentence resides. Like:
<span>A sentence residing within a span</span>
Is it possible to get the coordinates of the starting and ending letters of that sentence i.e "A" and "n" respectively on a Javascript event like onmouseover
attached with that span?
The span is resides within a div
. So
case - I: for the sentence
This is a sentence inline
The top and left is evaluated with respect to the letter "T" of the word "This".
case - II: for the sentence
... ... ... ... ... ... This is
a sentence broke into two lines.
The top is evaluated with respect to the letter "T" of the word "This" and the left is evaluated with respect to "a".
Is there any way to get the coordinates of the two terminals of a sentence?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
span.getBoundingClientRect()
? 文档span.getBoundingClientRect()
? docs如果您想要句子第一个字符的左上角和最后一个字符的右下角的坐标,可以使用 元素的
getClientRects()
方法。 所有主要浏览器的最新版本均支持。请注意,这些坐标是相对于视口而不是文档的。如果您需要使用这些坐标来定位其他元素,则需要考虑滚动。
If you want the coordinates of the top left of the first character and the bottom right of the last character of the sentence, you can use the
getClientRects()
method of the element. It's supported in recent versions of all major browsers.Note that these coordinates are relative to the viewport rather than the document. If you need to position other elements using these coordinates, you'll need to account for scrolling.