如何在光标开始和文本区域结束之间获取文本

how to get text in between cursor start and end of textarea

本文关键字:结束 区域 之间 获取 取文本 文本 光标 开始      更新时间:2023-09-26

如何在html页面的文本区域的光标开始和结束之间获取文本。

有人能帮我吗?我已经找过了,但没有找到任何相关的答案。

任何函数,如光标开始和光标结束。

可以使用以下吗

var selection = rangy.getSelection(),  // Whole lot of information, supports
                                       // multi-selections too.
    start   = selection.anchorOffset,  // Start position
    end     = selection.focusOffset;   // End position

演示

<textarea cols="50" rows="10" id="tarea"></textarea>
<button id="btn">get text</button>


$('#btn').on('click',function(){
    cursor = $('#tarea').prop("selectionStart");
    area = $('#tarea');
    console.log(area.val().substring(cursor,area.val().length));
});