在光标所在的TinyMCE编辑器中插入文本

时间:2022-10-06 09:54:31

I've been trying to insert text into the TinyMCE Editor at the focused paragraph element (<p>) exactly where the cursor is but got no luck!!

我一直试图在聚焦的段落元素(

)中将文本插入到TinyMCE编辑器中,正好是光标所在但没有​​运气!

var elem = tinyMCE.activeEditor.dom.get('tinymce');
var child = elem.firstChild;
while (child) {
    if (child.focused) {
        $(child).insertAtCaret("some text");
    }
    child = child.nextSibling;
}

If anyone has any idea on how to solve this I'll be very thankful.

如果有人知道如何解决这个问题,我将非常感激。

2 个解决方案

#1


77  

You should use the command mceInsertContent. See the TinyMCE documentation.

您应该使用命令mceInsertContent。请参阅TinyMCE文档。

tinymce.activeEditor.execCommand('mceInsertContent', false, "some text");

#2


9  

The above answer is good, but it is worth pointing out that this can be used to insert any HTML.

上面的答案很好,但值得指出的是,这可以用来插入任何HTML。

For example:

例如:

tinymce.activeEditor.execCommand('mceInsertContent', false, " <b>bolded text</b> ");

will insert bolded text at the current cursor location.

将在当前光标位置插入粗体文本。

Some other interesting observations:

其他一些有趣的观察:

mceInsertRawHTML also works, but tends to put the cursor at the beginning of the current line in my version of tinyMCE, but ymmv.

mceInsertRawHTML也可以工作,但是往往会把光标放在我的tinyMCE版本的当前行的开头,但是ymmv。

mceReplaceContent works as well, but in my case did not work well when the cursor was at the end of the current content.

mceReplaceContent也可以,但在我的情况下,当光标位于当前内容的末尾时,效果不佳。

Again, see the documentation for more information.

再次,请参阅文档以获取更多信息。

#1


77  

You should use the command mceInsertContent. See the TinyMCE documentation.

您应该使用命令mceInsertContent。请参阅TinyMCE文档。

tinymce.activeEditor.execCommand('mceInsertContent', false, "some text");

#2


9  

The above answer is good, but it is worth pointing out that this can be used to insert any HTML.

上面的答案很好,但值得指出的是,这可以用来插入任何HTML。

For example:

例如:

tinymce.activeEditor.execCommand('mceInsertContent', false, " <b>bolded text</b> ");

will insert bolded text at the current cursor location.

将在当前光标位置插入粗体文本。

Some other interesting observations:

其他一些有趣的观察:

mceInsertRawHTML also works, but tends to put the cursor at the beginning of the current line in my version of tinyMCE, but ymmv.

mceInsertRawHTML也可以工作,但是往往会把光标放在我的tinyMCE版本的当前行的开头,但是ymmv。

mceReplaceContent works as well, but in my case did not work well when the cursor was at the end of the current content.

mceReplaceContent也可以,但在我的情况下,当光标位于当前内容的末尾时,效果不佳。

Again, see the documentation for more information.

再次,请参阅文档以获取更多信息。