如何在 tinymce 中找到节点的结尾



我想在 tinymce 中下一个结束标签的末尾插入一个文本。例如。 <p>This is <span>This is a s|pan</span>paragraph</p> ."|"显示我的光标位置,我需要将光标放在'</span>|'之后。

以下是您需要执行的操作的大纲:

// get a reference to the Editor instance
// (depending on your scenario, this may come from a different place)
var editor = tinymce.activeEditor;
// get the node your cursor is in - the one you want to insert after
var endNode = editor.selection.getEnd();
// move the selection after the current node
editor.selection.select(endNode).collapse(false);
// insert your content
editor.selection.setContent("abc");

您可以使用 API 文档来实际使其正常工作。我上面写的内容严格基于文档 - 因为我现在没有时间测试它 - 所以它可能不会即插即用。

将之前的答案和注释编译成一个代码块...

// Get a reference to the Editor instance
// (depending on your scenario, this may come from a different place)
var editor = tinymce.activeEditor;
// get the node your cursor is in - the one you want to insert after
var endNode = editor.selection.getEnd();
// move the selection after the current node
editor.selection.select(endNode);
editor.selection.collapse(false);
// insert your content
editor.selection.setContent("abc");

最新更新