简体   繁体   English

在javascript中获取x和y坐标中的插入位置

[英]Get caret position in x and y coordinates in javascript

I am trying to find the current caret position (the blinking |) in x and y coordinates. 我试图在x和y坐标中找到当前的插入位置(闪烁|)。 Does anyone have a way of doing this? 有没有人有办法做到这一点? I really don't know where to get started. 我真的不知道从哪里开始。

I know this is possible in javascript, as there is a glitchy jQuery plugin for it, but I want to do it without jQuery. 我知道这在javascript中是可能的,因为它有一个小故障的jQuery插件,但我想在没有jQuery的情况下这样做。

More specifically, I am interested in mimicking the way Google Instant displays the #1 selection as grey text. 更具体地说,我有兴趣模仿Google Instant将#1选项显示为灰色文本的方式。 I plan on doing this for both textfields and textareas by retrieving the caret position (like I mentioned above), then placing the text using absolute positioning. 我计划通过检索插入位置(如上所述),然后使用绝对定位放置文本来为textfields和textareas执行此操作。 If someone has a better way to do it, let me know. 如果有人有更好的方法,请告诉我。

Thanks! 谢谢!

I would say that you're better off putting the entire guessed string (including the text typed so far) as a background label div and make the input box transparent. 我会说你最好将整个猜测的字符串(包括到目前为止输入的文本)作为背景标签div,并使输入框透明。

Positioning the input box and the div ought to be trivial. 定位输入框和div应该是微不足道的。 That way, you can make the label gray and it'll give the same effect. 这样,您可以将标签设置为灰色,它也会产生相同的效果。

Would that work? 那会有用吗?

EDIT: checked what google instant does. 编辑:检查谷歌即时功能。 They have two input boxes positioned on top of each other and the guess is populated in the background input 它们有两个相互叠加的输入框,并在背景输入中填充猜测

您需要注意鼠标移动事件并从移动中获取坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM