You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Froala编辑器无法在当前光标位置插入内容问题求助

解决Froala编辑器光标位置插入文本的问题

你当前代码里的editor.html.insert()方法传入了true作为第二个参数,这个参数的作用是强制把内容追加到编辑器末尾,这就是文本没出现在光标位置的原因。

修正方案很简单:

  • 保留你已有的selection.save()和selection.restore()逻辑,这部分能确保光标位置被正确记录和恢复。
  • 修改insertVariable函数,去掉editor.html.insert()的第二个参数true(或者传入false),默认情况下该方法会在当前光标/选区位置插入内容。

修正后的完整代码:

var editor = new FroalaEditor('#docEditor', {
    events: {
        blur() {
            this.selection.save();
        }
    },
});

function insertVariable(text) {
    editor.selection.restore();
    editor.html.insert("->" + text + "<- ");
}

另外,也可以使用Froala专门的选区插入方法editor.selection.insert(),效果一致,代码如下:

function insertVariable(text) {
    editor.selection.restore();
    editor.selection.insert("->" + text + "<- ");
}

内容的提问来源于stack exchange,提问作者Jay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 07:10:33