I know the mouse can be located by position with the event.clientX and event.clientY features but is there any way to tell which character # of the document the curser is on?
views:
76answers:
1
+2
A:
If you can deal with the potential performance issues and complications around font-sizing, styling, and width of the areas:
- Find the smallest element containing text.
- Render another copy of the element off-screen (absolute position, way up and left?)
- Measure the off-screen element's height and width.
- Remove characters until one line is gone.
- Remember which character started the line.
- If the new height of the element is no longer tall enough to cover the y-coordinate translation, then you just got rid of the line that the mouse was over.
- Add characters from the correct line, one at a time.
- Measure the width of the off-screen element.
- When the translated x-coordinate finally fits in the element, you found the character!
Warning! This method could become quite difficult inside a complicated page, or in a place where you don't know all the details of the html structure around the text you're examining.
John Fisher
2010-07-02 19:48:27