Textarea X/Y 插入符坐标 - jQuery 插件

发布于 2024-09-15 00:24:38 字数 80 浏览 4 评论 0原文

我希望在按下按键时获取文本区域内插入符号的 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-09-22 00:24:38

AFAIK 唯一可行的方法是:

  1. 将 TEXTAREA 的内容附加到 DIV
  2. 将 DIV 附加到 DOM
  3. 将 SPAN 放置在 DIV 内的 插入符号的字符偏移量
  4. 获取 SPAN 的偏移量 ($(span).offset()...) 并减去 DIV 的偏移量(取决于您想要相对于文本区域还是相对于文本区域的 x/y 坐标) 。

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

The only viable way of doing this, AFAIK:

  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)

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

哆兒滾 2024-09-22 00:24:38

我为此编写了 这个 jQuery 插件。

I wrote this jQuery plugin for exactly that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文