3

我正在尝试在当前光标位置插入一些内容或替换 TinyMCE 编辑器实例中的选定内容。我正在使用这个:

tinyMCEPopup.editor.selection.setContent(content);

content我要插入的 HTML 字符串在哪里。

它适用于 Firefox、Opera 和 Chrome,但不适用于 IE。相反,它在编辑器的开头插入内容,而不是选择。我也试过:

tinyMCEPopup.execCommand('mceInsertContent', false, content);

同样,IE 中的行为相同。用于插入特殊字符的 TinyMCE 代码使用了上面的函数并且有效!我已经尝试在我的插件中复制它,但仍然没有乐趣......

function insertChar(chr) {
    tinyMCEPopup.execCommand('mceInsertContent', false, '&#' + chr + ';');

    // Refocus in window
    if (tinyMCEPopup.isWindow)
        window.focus();

    tinyMCEPopup.editor.focus();
    tinyMCEPopup.close();
}

更新

仍然无法在 IE 中运行,Opera 甚至没有在工具栏中显示我的插件按钮(尽管这可能是一个单独的问题)!我现在也尝试了 jQuery 插件,让编辑器加载,但无法调用 mceInsertContent 方法,即使使用这个:

$('#my-editor-id').tinymce().execCommand('mceInsertContent', false, content);

出现错误:“对象没有方法:tinymce” - 也许我不能从弹出窗口中调用它?!

4

3 回答 3

4

你试过打电话吗:

tinyMCEPopup.restoreSelection(); 

打电话之前

tinyMCEPopup.execCommand('mceInsertContent', false, '&#' + chr + ';');
于 2012-11-06T22:04:22.620 回答
3

问题是当您单击编辑器外部的按钮或链接等其他内容时,TinyMCE 会丢失当前光标位置,从而导致其失去焦点。
要解决此问题,请使用按钮或链接的 mousedown 事件。这样,您可以在失去焦点之前执行插入操作。

$("#insert_link").mousedown(function (e) {
    var editor = tinyMCE.get("editor");
    editor.execCommand('mceInsertContent', false, "Insert Value");
    e.preventDefault();
});
于 2013-05-14T06:41:58.007 回答
2

在类似情况下对我有用的东西,以及这里可能的解决方案 - 使用

editor.selection.getBookmark();

在失去编辑的注意力之前

editor.selection.moveToBookmark();

在插入内容之前。

问题在于,在 FF 中,当您将光标放在包含文本的文本字段中的某个位置时,然后单击其他位置并返回(即通过按 Tab),插入符号将显示在失去焦点之前的位置。在 IE 中它会回到开头。我希望这有帮助。

于 2014-03-02T19:55:59.593 回答