简体   繁体   English

jQuery获取textarea光标/插入符号X,Y位置并在下面显示DIV

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

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" 我正在尝试在Gmail / Google Docs富文本格式所见即所得版本中实现“更改/删除链接”之类的功能,当您键入URL时,div下方会显示“ Goto Link,Change,Remote”

How could I write something like that using jQuery? 我如何使用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? 如何确保DIV出现在单词的开头?

Thank you in advance! 先感谢您!

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

What it does: 它能做什么:

  • Creates div positioned near hyperlink. 创建位于超链接附近的div。
  • Looks like Google docs box. 看起来像Google文档框。
  • Ability to change text and url. 能够更改文本和网址。
  • Remove is implemented. 删除已实现。

What it does not do: 它不做什么:

  • Work on textarea. 在textarea上工作。 Textareas don't support html as they are plain text. Textareas不支持html,因为它们是纯文本。 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. 由于上述项目而无法使用。

You're suggesting you're building a WYSIWYG editor. 您建议您正在构建所见即所得的编辑器。 Are you sure you want to use a textarea? 您确定要使用文本区域吗? Textareas don't support HTML. Textareas不支持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 . 为了回答您以后的评论 ,获取插入符在文本区域中的(x,y)位置的最佳方法是使用textarea-caret-position插件

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

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