简体   繁体   English

获取/使用HTML文本区域内的选定位置

[英]Getting/Using selected position inside HTML textarea

   <textarea id="editor"></textarea>
   <input type="button" value="Insert Comment" onclick="document.getElementById('editor').value+='<'+'!--    -->\n'"/>

The program I created (summed up to the basics above) works as intended, however I was wondering if I could improve it. 我创建的程序(总结了以上基础知识)可以按预期工作,但是我想知道是否可以改进它。 There is a textarea where you can type things, and upon clicking the input button it adds an HTML comment string to the text inside the textarea. 有一个文本区域,您可以在其中键入内容,单击输入按钮后,它将在文本区域内的文本上添加HTML注释字符串。 I want it to add the < ! 我希望它添加<! -- --> to the part of the textarea that was is selected (as in the place where text would show up next if you typed). -->到选定的文本区域部分(如在您键入文本的地方显示)。 Is that possible in raw Javascript preferably, or is there a library that would allow this to be possible? 最好在原始Javascript中做到这一点,还是有一个允许这样做的库?

Finding the cursor position in a text area is a fairly commonly asked question; 在文本区域中找到光标位置是一个相当普遍的问题。 inserting just less so. 插入少了点。 Take a look at these. 看看这些。 There's some redundancy between them, but the discussion might be useful (or one might explain it better for you). 它们之间有一些冗余,但是讨论可能会有用(或者可能会为您更好地解释)。

Insert text into textarea at cursor position (Javascript) 将文本插入到光标所在位置的textarea中(JavaScript)

How do you get the cursor position in a textarea? 如何获得文本区域中的光标位置?

How do you get the cursor position in a textarea? 如何获得文本区域中的光标位置?

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

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