jQuery get textarea cursor/caret X,Y position and show a DIV underneath

一笑奈何 提交于 2019-12-24 09:22:52

问题


I am trying to implement something like the "Change/Remove Link" in Gmail/Google Docs richtext WYSIWYG edtior, when you type a URL, a div shows underneath it says "Goto Link, Change, Remote"

How could I write something like that using jQuery?

  1. How to get row and column of cursor?
  2. how can I calculate font width and height (especially non-fixed width font with some Bold/Italic style)
  3. How to make sure the DIV appears at the start of a word?

Thank you in advance!


回答1:


Answer: http://jsfiddle.net/morrison/57BR3/

What it does:

  • Creates div positioned near hyperlink.
  • Looks like Google docs box.
  • Ability to change text and url.
  • Remove is implemented.

What it does not do:

  • Work on textarea. Textareas don't support html as they are plain text. This is a complex process to work-around. Find a library, then implement my answer.
  • Open when your cursor gets moved onto it by arrowkeys. Doesn't work because of above item.



回答2:


You're suggesting you're building a WYSIWYG editor. Are you sure you want to use a textarea? Textareas don't support HTML. To answer your later comment, the best way to get the (x, y) position of the caret in a text area is to use the textarea-caret-position plugin.



来源:https://stackoverflow.com/questions/5752147/jquery-get-textarea-cursor-caret-x-y-position-and-show-a-div-underneath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!