textarea光标问题

现在找到的办法都是获取光标的绝对位置或相对位置,有没有办法获取光标前的所有文字或内容?

拉丁的传说
浏览 1133回答 1
1回答

DIEA

支持FF,Chrome<!DOCTYPE html><html><head><meta charset="UTF-8"><title>获得光标位置之前的文本</title></head><body>&nbsp; &nbsp; <textarea id="source" rows="10" cols="50">青玉案·元夕东风夜放花千树。更吹落、星如雨。宝马雕车香满路。凤箫声动,玉壶光转,一夜鱼龙舞。蛾儿雪柳黄金缕。笑语盈盈暗香去。众里寻他千百度。蓦然回首,那人却在,灯火阑珊处。</textarea>&nbsp; &nbsp; <textarea id="select" rows="10" cols="50" disabled></textarea>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; setInterval(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var source = document.getElementById('source');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var select = document.getElementById('select');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var start = source.selectionStart | 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var end = source.selectionEnd | 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (start == end) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select.value = source.value.substring(start, end);&nbsp; &nbsp; &nbsp; &nbsp; }, 200);&nbsp; &nbsp; </script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript