检索 DOM 文本节点位置

发布于 2024-07-10 03:20:58 字数 43 浏览 10 评论 0原文

是否可以检索文本节点的几何位置(即距父元素、页面等的顶部/左侧偏移量)?

Is it possible to retrieve the geometric position (i.e. top/left offsets from either the parent element, the page, etc) of a text node?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

孤云独去闲 2024-07-17 03:20:58

不直接。 TextNode 没有用于测量视口定位的原始 IE 偏移*(和类似)扩展。

仅在 IE 上,您可以创建一个 TextRange 对象,费力地尝试将其与 TextNode 的边界对齐,然后测量 TextRange 的boundingLeft/boundingTop,并将其添加到父元素的位置(通过通常的方式获得) 。 然而,对于可能不稳定的单浏览器解决方案来说,这是一项艰巨的工作。

您可能能够摆脱的另一种方法是将文本包装在 span 元素中(在文档中,或者通过脚本动态和/或临时),并使用 span 来测量位置,假设它没有拾取任何额外的内容可能影响位置的样式。 但请注意,包裹的跨度可能不会给出预期的右/底值; 根据您想用它做什么,您可能最终会包装第一个和最后一个字符或其他一些安排。

总结一下:呃,没什么好。

Not directly. TextNode does not have the originally-IE offset* (and similar) extensions for measuring on-viewport positioning.

On IE only, you could create a TextRange object, laboriously attempt to align it with the bounds of the TextNode, then measure the boundingLeft/boundingTop of the TextRange, and add it to the position of the parent element (obtained by the usual means). However this is a bunch of work for a potentially-wobbly single-browser solution.

Another approach you might be able to get away with would be wrapping the text in a span element (either in the document, or dynamically and/or temporarily by script), and using the span to measure positioning, assuming it's not picking up any additional styles that may affect the position. Note however that a wrapped span may not give the expected right/bottom values; depending on what you want to do with it, you might end up wrapping the first and last character or some other arrangement.

In summary: urgh, nothing good.

暮光沉寂 2024-07-17 03:20:58

今天您可以通过 Range.getBoundingClientRect()

// Get your text node
const el = document.querySelector('strong')
const textNode = el.firstChild;

// Get coordinates via Range
const range = document.createRange();
range.selectNode(textNode);
const coordinates = range.getBoundingClientRect()

console.log(coordinates);
/* Demo CSS, ignore, highlights box coordinates */ body{max-width:300px}strong{position:relative}strong,strong:before{border:solid 1px red}strong:before{content:'';position:absolute;right:100%;bottom:100%;width:100vw;height:100vh;pointer-events:none}
The red lines will show you the coordinates bounding box of the <strong>selected textnode</strong>.

Today you can via Range.getBoundingClientRect().

// Get your text node
const el = document.querySelector('strong')
const textNode = el.firstChild;

// Get coordinates via Range
const range = document.createRange();
range.selectNode(textNode);
const coordinates = range.getBoundingClientRect()

console.log(coordinates);
/* Demo CSS, ignore, highlights box coordinates */ body{max-width:300px}strong{position:relative}strong,strong:before{border:solid 1px red}strong:before{content:'';position:absolute;right:100%;bottom:100%;width:100vw;height:100vh;pointer-events:none}
The red lines will show you the coordinates bounding box of the <strong>selected textnode</strong>.

罪#恶を代价 2024-07-17 03:20:58

相对于视口的文本节点

function getBoundingClientRectText(textNode) {
  const range = document.createRange()
  range.selectNode(textNode)
  return range.getBoundingClientRect()
}

相对于元素的文本节点

function getTextOffsetRelativeToElement(element, textNode) {
  const elementRect = element.getBoundingClientRect()
  const range = document.createRange()
  range.selectNode(textNode)
  const textRect = range.getBoundingClientRect()
  return {
    top: textRect.top - elementRect.top,
    left: textRect.left - elementRect.left
  }
}

相对于文本节点父元素的字符偏移

function getCharOffset(textNode, offset) {
  const parentRect = textNode.parentElement.getBoundingClientRect()
  const range = document.createRange()
  range.setStart(textNode, offset)
  range.setEnd(textNode, offset + 1)
  const charRect = range.getBoundingClientRect()
  return {
    top: charRect.top - parentRect.top,
    left: charRect.left - parentRect.left
  }
}

Text node relative to viewport

function getBoundingClientRectText(textNode) {
  const range = document.createRange()
  range.selectNode(textNode)
  return range.getBoundingClientRect()
}

Text node relative to an element

function getTextOffsetRelativeToElement(element, textNode) {
  const elementRect = element.getBoundingClientRect()
  const range = document.createRange()
  range.selectNode(textNode)
  const textRect = range.getBoundingClientRect()
  return {
    top: textRect.top - elementRect.top,
    left: textRect.left - elementRect.left
  }
}

Character offset relative to text node's parent element

function getCharOffset(textNode, offset) {
  const parentRect = textNode.parentElement.getBoundingClientRect()
  const range = document.createRange()
  range.setStart(textNode, offset)
  range.setEnd(textNode, offset + 1)
  const charRect = range.getBoundingClientRect()
  return {
    top: charRect.top - parentRect.top,
    left: charRect.left - parentRect.left
  }
}

三月梨花 2024-07-17 03:20:58

看看 这篇文章 - 它使用 offsetParent 属性递归地计算出偏移量。

对于像这样的浏览器变化的东西,我总是推荐 jquery 而不是滚动你自己的方法。 jquery CSS 函数 似乎有您需要的方法!

Have a look at this article - it uses the offsetParent property to recursively figure out the offset.

I would always recommend jquery over rolling your own methods for browser-varying stuff like this. The jquery CSS functions seem to have the methods you need!

别挽留 2024-07-17 03:20:58

没有任何内置的跨浏览器方法可以做到这一点。 我会
将 jQuery 与 Dimensions 插件结合使用: http://brandonaaron.net/docs/dimensions/

它是跨浏览器,将为您提供高度、宽度和 x & y 偏移等。

There isn't any built-in, cross-browser way to do this. I would
use jQuery with the Dimensions plugin: http://brandonaaron.net/docs/dimensions/

It is cross-browser and will give you height, width and x & y offsets, among others.

心碎的声音 2024-07-17 03:20:58

这些天有办法。 一种方法: getBoundingRectangle: https://developer.mozilla.org/ en-US/docs/Web/API/Element.getBoundingClientRect

These days there are ways. One way: getBoundingRectangle: https://developer.mozilla.org/en-US/docs/Web/API/Element.getBoundingClientRect

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文