向编辑器文本框等发送文本

function sendTextToEditable(element, text) {
  // 聚焦到编辑器
  element.focus();
  
  // 插入文本
  document.execCommand('insertText', false, text);
  
  // 或直接操作 DOM(适用于简单场景)
  // element.textContent += text;
  
  // 触发 input 事件
  const inputEvent = new Event('input', { bubbles: true });
  element.dispatchEvent(inputEvent);
}

// 使用示例
const editorElement = document.querySelector('div[contenteditable="true"]');
sendTextToEditable(editorElement, "Hello World");
image.png

留下你的脚步
推荐阅读