一. 获取光标位置:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
getCursorPosistion(dom) { if (!document.selection) { // 非ie,当选中范围时返回数组 const start = dom.selectionStart; const end = dom.selectionEnd; return start === end ? start : [start, end]; } else { // ie dom.focus(); var selectRange = document.selection.createRange(); selectRange.moveStart("character", -dom.value.length); return selectRange.text.length; } }, |
二. 设置光标位置:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
setCursorPosition(dom, pos) { if (!document.selection) { // 非ie dom.setSelectionRange(pos, pos); dom.focus(); } else { // ie var range = dom.createTextRange(); range.moveEnd("character", pos); range.moveStart("character", pos); range.select(); } } |
三. 获取选中文字:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
四. 选中特定范围的文本:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
五. 在光标后插入文本:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
博客主要介绍了文本编辑中的相关操作,包括获取光标位置、设置光标位置、获取选中文字、选中特定范围的文本以及在光标后插入文本,这些操作在信息技术的文本编辑场景中较为关键。
2198

被折叠的 条评论
为什么被折叠?



