为用户站立的文本框添加值

Kfi*_*deh 5 html javascript

我想在文本框中的字符中添加元音。我创建了输入和一个将元音值添加到文本框中的函数:

<textarea dir="rtl" id="text" runat="server" name="text" rows="5" cols="30""></textarea> <br />
<input type="button" style="height:28px;width:42px;" id="Dagesh" value="???" onclick="AddNikud(&#39;?&#39;)"/>

<script>
function AddNikud(nikud) {
   document.getElementById('text').value += nikud; 
   document.getElementById('text').focus();
}
</script>
Run Code Online (Sandbox Code Playgroud)

问题是 - 该函数将元音添加到文本框中的最后一个字符中。我希望在用户的键盘光标所在的位置之后输入元音。

例如:

文本框:你好我(用户在这里)rld!

元音值应该从 'o' 的右边开始。

提前致谢!

IAf*_*sov 4

为了获得卡雷特的位置,我们有以下属性selectionStart

const textarea = document.getElementById('text');
textarea.value = textarea.value.substring(0, textarea.selectionStart) + nikud + textarea.value.substring(textarea.selectionStart, textarea.value.length);
textarea.focus();
Run Code Online (Sandbox Code Playgroud)