使用javascript在文本区域中的光标的当前x,y坐标 [英] current x,y coordinates of cursor in text area using javascript
本文介绍了使用javascript在文本区域中的光标的当前x,y坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取当前的x&使用javascript在文本区域内CURSOR的y位置. 该事件是Keyup事件,而不是鼠标事件.
How can i get current x & y position of my CURSOR within a text area using javascript. The event is a Keyup event, not a mouse event.
我不是在寻找当前光标位置的字符,而是x,y坐标.
I am not looking for the current cursor position in terms of charecter but x , y coordinates.
推荐答案
我能想到的唯一可靠的方法是:
The only somewhat reliable method I can think of is this:
- 创建一个
<span>
屏幕外(绝对位于左侧) - 赋予它与输入元素相同的字体特征.
- 用来自输入元素但插入符号之前的文本连续填充
- 使用JavaScript库查找屏幕外跨度的像素宽度
- 使用它来确定插入符号相对于输入元素左侧的位置.
- Create a
<span>
offscreen (absolutely positioned way to the left) - Give it the same font characteristics as the input element.
- Continuously populate it with the text from the input element but before the caret
- Use a javascript library to find the pixel width of the offscreen span
- Use that to determine where the caret is relative to the left side of the input element.
试一试,我很想听听结果.
Give it a shot, I'd love to hear how it turns out.
这篇关于使用javascript在文本区域中的光标的当前x,y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文