简体   繁体   English

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

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

how to get text in between cursor start and end of textarea in html page. 如何在html页面中textarea的光标开始和结束之间获取文本。

Can someone help me please, i have searched for this but i didnt find any related answers. 有人可以帮我吗,我已经搜索过了,但是没有找到任何相关的答案。

any functions like cursor start and cursor end. 任何功能,例如游标开始和游标结束。

can the following could be used 可以使用以下内容吗

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

Demo 演示版

<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));
});

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

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