---
title: "insertContent 命令"
description: "使用 Tiptap 中的 insertContent 命令以纯文本、HTML 或 JSON 向文档添加内容。在我们的文档中了解更多信息！"
canonical_url: "https://tiptap.zhcndoc.com/editor/api/commands/content/insert-content"
---

# insertContent 命令

使用 Tiptap 中的 insertContent 命令以纯文本、HTML 或 JSON 向文档添加内容。在我们的文档中了解更多信息！

`insertContent` 命令将传递的值添加到文档中。

另请参阅: [setContent](https://tiptap.zhcndoc.com/editor/api/commands/content/set-content.md), [clearContent](https://tiptap.zhcndoc.com/editor/api/commands/content/clear-content.md)

## 参数

`value: Content`

该命令非常灵活，可以接受纯文本、HTML 甚至 JSON 作为值。

## 使用 insertContent 命令

```js
// 纯文本
editor.commands.insertContent('示例文本')

// HTML
editor.commands.insertContent('<h1>示例文本</h1>')

// 去除 HTML 中的空白
editor.commands.insertContent('<h1>示例文本</h1>', {
  parseOptions: {
    preserveWhitespace: false,
  },
})

// JSON/节点
editor.commands.insertContent({
  type: 'heading',
  attrs: {
    level: 1,
  },
  content: [
    {
      type: 'text',
      text: '示例文本',
    },
  ],
})

// 一次插入多个节点
editor.commands.insertContent([
  {
    type: 'paragraph',
    content: [
      {
        type: 'text',
        text: '第一段',
      },
    ],
  },
  {
    type: 'paragraph',
    content: [
      {
        type: 'text',
        text: '第二段',
      },
    ],
  },
])
```
