简体   繁体   English

Javascript-在textarea中的光标位置插入图片标签

[英]Javascript - insert image tag at the position of the cursor in the textarea

I know there are a few posts with the similar title, but they don't seem to refer to the same question. 我知道有几篇标题相似的文章,但它们似乎并没有提到相同的问题。 I'm trying to put the image tag at the position of the cursor inside of the specific textarea from withing the popup instantiated by the button from the same page as the textearea. 我试图通过与textearea同一页面上的按钮实例化的弹出窗口,将图像标签放置在特定文本区域内的光标位置。

At the moment I simply append the image tag to the end of the content in textarea like so: 目前,我只是将image标签附加到textarea中内容的末尾,如下所示:

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

I've found one post here: How To insert an image at cursor position in tinymce , but obviously this one refers to the tinymce, which has some built in functions available. 我在这里找到了一篇文章: 如何在tinymce中的光标位置插入图像 ,但是显然,这是指tinymce,它具有一些内置函数。

Any thoughts? 有什么想法吗?

You could use selectionStart read more about it at: https://developer.mozilla.org/en-US/docs/DOM/HTMLTextAreaElement 您可以使用selectionStart进一步了解它, 网址为: https : //developer.mozilla.org/en-US/docs/DOM/HTMLTextAreaElement

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

Also see Caret position in textarea, in characters from the start 另请参阅文本区域中插入符号的位置(从头开始的字符)

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

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