查找文本区域中的插入符号位置(以像素为单位)
我想知道是否有可能找到文本区域内插入符号相对于整个页面的确切位置(以像素为单位)。例如,如果我在文本框中输入了 This is text
,我想知道从屏幕左上角到插入符号的像素。
其格式为 X: 200 Y: 100。这样我就可以定位浮动 div。这需要在 javascript 中动态完成。
谢谢大家
I was wondering if it is possible to find the exact location of the caret inside a textarea in pixels in relation to the entire page. For instance, if I have typed This is text
into my text box, I would like to know the pixels from the top left of the screen to the caret.
It would be in the form X: 200 Y: 100. This is so I can position a floating div. This needs to be done dynamically in javascript.
Thanks guys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个“原始代码”至少可以在 IE 中运行。
使用该代码,您可以将
的定位不太精确,但在
This "raw code" works at least in IE.
Using the code you can put your
<TEXTAREA>
where ever you want in page, and the<DIV id="db">
will follow it. Even despite of the scrolling position of the page. You can fix the position of<DIV>
by changing the literal numbers atd.style...6
-statements.Positioning of the
<DIV>
is not quite exact, but gets better when using fixed-width font in<TEXTAREA>
.这是来自 输入类型中插入符位置(以像素为单位)的简单组合文本(不是文本区域) 、 插入符位置textarea,从头开始的字符和document.getElementById vs jQuery $()获取 jQuery 中
元素的插入符号位置。在其内部单击时它可以工作,但使用箭头或键入移动插入符号时则不起作用。
我们使用
var inputter = document.getElementById('someid')
而不是var inputter = $('#someid')[0];
这是一个 < a href="http://jsfiddle.net/YbSxf/32/" rel="nofollow noreferrer">FIDDLE。
Here is a simple mix of ideas from Caret position in pixels in an input type text (not a textarea) , Caret position in textarea, in characters from the start and document.getElementById vs jQuery $() to get the caret position in jQuery for an
<input>
element. It works when clicking inside it, but not when moving the caret using the arrows or typing.Instead of
var inputter = document.getElementById('someid')
we usevar inputter = $('#someid')[0];
Here is a FIDDLE.