【问题标题】:Javascript - insert image tag at the position of the cursor in the textareaJavascript - 在文本区域中光标的位置插入图像标签
【发布时间】:2011-09-26 07:55:48
【问题描述】:

我知道有一些标题相似的帖子,但它们似乎没有提到同一个问题。 我正在尝试将图像标签放在特定文本区域内的光标位置,以使用与文本区域相同页面的按钮实例化的弹出窗口。

目前我只是将图像标签附加到 textarea 中内容的末尾,如下所示:

window.opener.document.getElementById('textarea_id').value += '<img .... />';

我在这里找到了一个帖子:How To insert an image at cursor position in tinymce,但显然这个帖子指的是 tinymce,它有一些可用的内置功能。

有什么想法吗?

【问题讨论】:

  • 哦,我可以 - 检查下面的答案。
  • 好的 - 我明白了,但在我的情况下这很好,因为我以稍微不同的方式呈现它 - 谢谢。

标签: javascript cursor cursor-position


【解决方案1】:

您可以使用 selectionStart 阅读更多信息:https://developer.mozilla.org/en-US/docs/DOM/HTMLTextAreaElement

window.opener.document.getElementById('textarea_id').selectionStart

另见Caret position in textarea, in characters from the start

【讨论】:

    猜你喜欢
    • 2011-07-09
    • 2013-08-03
    • 2012-06-20
    • 2011-03-19
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多