获取选定的文本位置 [英] Getting selected text position

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

问题描述



$ $ $ $ $ $ $ $ $> window.getSelection();

现在我需要在按下自定义键时在文本上方显示工具提示(请注意,鼠标可以不要超过文本),所以为了做到这一点,我需要所选文本的绝对位置。



有没有办法做到这一点,也许包装标签中的文字,然后得到补偿?



任何想法?






PS:必须使用chrome,而不是所有浏览器。



PS2:对不起我的蹩脚英语。 p>

解决方案

最简单的方法是在选择的开始或结束处插入临时标记元素并获取其位置。我已经演示了如何在Stack Overflow之前执行此操作:如何在用户文本选择旁边放置元素?


Currently a I'm getting the selected text in the browser doing this:

window.getSelection();

Now I need to show a tooltip above that text when pressing a custom key(note that the mouse could not be over the text anymore), so in order to do that I need the absolute position of that selected text.

Is there a way to do that, maybe wrapping that text inside a tag and then getting the offsets?

Any Idea?


PS: Have to work from chrome, not for all browsers.

PS2: Sorry for my crappy English.

解决方案

The easiest way is to insert a temporary marker element at the start or end of the selection and get its position. I've demonstrated how to do this before on Stack Overflow: How can I position an element next to user text selection?

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

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