---
title: "setTextSelection 命令"
description: "使用 setTextSelection 命令来控制并设置文本选择到指定范围或位置。详细了解请查看我们的文档！"
canonical_url: "https://tiptap.zhcndoc.com/editor/api/commands/selection/set-text-selection"
---

# setTextSelection 命令

使用 setTextSelection 命令来控制并设置文本选择到指定范围或位置。详细了解请查看我们的文档！

如果你在编辑器的上下文中考虑选择，你很可能会想到文本选择。通过 `setTextSelection` 你可以控制文本选择并将其设置为指定的范围或位置。

另见: [focus](https://tiptap.zhcndoc.com/editor/api/commands/selection/focus.md), [setNodeSelection](https://tiptap.zhcndoc.com/editor/api/commands/selection/set-node-selection.md), [deleteSelection](https://tiptap.zhcndoc.com/editor/api/commands/selection/delete-selection.md), [selectAll](https://tiptap.zhcndoc.com/editor/api/commands/selection/select-all.md)

## 参数

`position: number | Range`

传入一个数字，或一个范围，例如 `{ from: 5, to: 10 }`。

## 使用 setTextSelection 命令

```js
// 将光标设置到指定位置
editor.commands.setTextSelection(10)

// 将文本选择设置为指定范围
editor.commands.setTextSelection({ from: 5, to: 10 })
```
