检索DOM文本位置 [英] Retrieving DOM textnode position

查看:92
本文介绍了检索DOM文本位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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



仅在IE上,您可以创建一个TextRange对象,费力地尝试将其与TextNode的边界对齐,然后测量TextRange的boundingLeft / boundingTop,并将其添加到父元素的位置(以通常方式获取)。然而,这是一个潜在的摇摆单浏览器解决方案的一大堆工作。



另一种可能被忽略的方法是将文本包围在跨度元素(在文档中,或者通过脚本动态地和/或临时地),并且使用跨度来测量定位,假设它没有拾取可能影响位置的任何附加样式。但是请注意,包装的跨度可能不会给出预期的右/右值;取决于你想要做什么,你可能会最后包装第一个和最后一个字符或其他一些安排。



总结:urgh,没有什么好的。 p>

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?

解决方案

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.

这篇关于检索DOM文本位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆