Textarea X / Y插入符号 - jQuery插件 [英] Textarea X/Y caret coordinates - jQuery plugin

查看:127
本文介绍了Textarea X / Y插入符号 - jQuery插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在键盘上的textarea中获得插入符号的X / Y坐标。我已经大力搜索但没有任何运气,似乎你可以得到位置,但不是屏幕上的X / Y坐标。

I'm looking to get the X/Y coordinates of the caret within a textarea on key down. I've searched vigorously but without any luck whatsoever, it seems you can get the position, but not the on-screen X/Y coordinates.

推荐答案

唯一可行的方法,AFAIK:

The only viable way of doing this, AFAIK:


  1. 将TEXTAREA的内容附加到DIV

  2. 将DIV附加到DOM

  3. 在DIV中将SPAN放在插入符号的字符偏移量

  4. 获取SPAN的偏移量( $( span).offset() ...)并减去DIV的偏移量(取决于你是否想要相对于textarea或页面的x / y坐标)

  1. Append the contents of the TEXTAREA to a DIV
  2. Append the DIV to the DOM
  3. Place a SPAN inside the DIV at the character offset of the caret.
  4. Take the offset of the SPAN ($(span).offset()...) and minus the offset of the DIV (depending on whether you want the x/y coords relative to the textarea or the page)

注意:将文本从TEXTAREA复制到DIV时,请确保复制可能影响偏移的相关CSS属性(宽度,高度,填充,边距,边框,字体大小,行高等)。

Note: When copying the text from the TEXTAREA to the DIV, make sure to copy relevant CSS properties that may affect the offset (width, height, padding, margin, border, font-size, line-height etc.).

这篇关于Textarea X / Y插入符号 - jQuery插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆