在javascript中获取x和y坐标中的插入符位置
我试图在 x 和 y 坐标中找到当前插入符号位置(闪烁的 |)。有人有办法做到这一点吗?我真的不知道从哪里开始。
我知道这在 javascript 中是可能的,因为有一个有问题的 jQuery 插件,但我想在没有 jQuery 的情况下做到这一点。
更具体地说,我有兴趣模仿 Google Instant 将 #1 选择显示为灰色文本的方式。我计划通过检索插入符位置(就像我上面提到的),然后使用绝对定位放置文本来对文本字段和文本区域执行此操作。如果有人有更好的方法,请告诉我。
谢谢!
I am trying to find the current caret position (the blinking |) in x and y coordinates. 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.
More specifically, I am interested in mimicking the way Google Instant displays the #1 selection as grey text. 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. If someone has a better way to do it, let me know.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想说,你最好将整个猜测的字符串(包括到目前为止输入的文本)作为背景标签 div 并使输入框透明。
定位输入框和 div 应该很简单。这样,您可以将标签设为灰色,并且会产生相同的效果。
那行得通吗?
编辑:检查谷歌即时做什么。它们有两个彼此重叠的输入框,猜测值填充在后台输入中
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.
Positioning the input box and the div ought to be trivial. 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
您需要注意鼠标移动事件并从移动中获取坐标。
You need to watch out for mouse move events and get the coordinates from the move.