简体   繁体   English

如何根据插入符号的当前位置选择文本?

[英]how to select text by the current position of the caret?

选择对象代表用户选择的文本范围或插入符号的当前位置 ”,那么如何通过插入符号的当前位置选择文本?

You need to create range and add it to the selection, with use of addRange . 您需要使用addRange创建范围并将其添加到选择中。

Code example: 代码示例:

  const sel = window.getSelection();
  const range = document.createRange();
  range.setStart(0, currentPos);
  sel.addRange(range);

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

相关问题 如何在textarea中的当前插入位置插入文本 - How to insert text at the current caret position in a textarea 如何获得SCEditor文本区域中的当前插入符位置? - How to get current caret position in a SCEditor textarea? JS-如何获取节点开头和当前插入符号(文本光标)位置之间的文本? - JS - how to get the text between the beginning of the node and current caret (text cursor) position? 替换contenteditable元素中的文本时如何将插入符号保持在当前位置 - How to leave the caret at its current position when replace a text in contenteditable element 如何在 JavaScript/Vue 的文本字段中获取插入符号的当前 position? - How do I get the current position of the caret in a text-field in JavaScript/Vue? 如何使用javascript在插入符号位置键入文本? - How to type text at the caret position using javascript? 如何根据插入位置突出显示文本? - How to highlight text based on caret position? 无法选择文本,无法在 Firefox 中定位插入符号 - Slatejs - Cant select text, cant position caret in Firefox - Slatejs 如何使用contentEditable从iframe中的当前插入位置获取像素偏移量 - How to get the pixel offset from the current caret position in an iframe with contentEditable 如何移动插入符号位置 - How to move caret position
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM