简体   繁体   中英

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. 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!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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