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



如何在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));
});

相关内容

最新更新