更新时间:2023-01-28 07:57:02
感谢您的帮助,但没有任何帮助.我找到了自己的解决方案,并且觉得需要共享它,因为我只找到了过时的解决方案,这是行不通的:(.
Thanks for the help, but nothing worked. I found my own solution, and I felt like sharing this was needed, because I only found outdated solutions, that did not work :(.
解决方案:
在您的TinyMCE初始化中,添加以下代码:
In your TinyMCE init add following code:
tinyMCE.init({
// General options
elements: elementid,
setup: function (ed) {
ed.onKeyUp.add(function (ed, e) {
actualCaretPositionBookmark = tinyMCE.activeEditor.selection.getBookmark();
});
ed.onClick.add(function (ed, e) {
actualCaretPositionBookmark = tinyMCE.activeEditor.selection.getBookmark();
});
}
其中"actualCaretPositionBookmark"是javascript中的全局变量,您所有的javascript代码都可以访问
Where "actualCaretPositionBookmark" is a global variable in javascript that can be accesed by all your javascript code
然后,当您想通过javascript将文本添加到TinyMCE编辑器中时,添加以下代码:
Then when you want to add text into your TinyMCE editor via javascript, add following code:
if (tinymce.isIE) {
tinyMCE.activeEditor.selection.moveToBookmark(actualCaretPositionBookmark);
tinyMCE.execCommand('mceInsertContent',false, 'My new text');
}else {
tinyMCE.execCommand('insertHTML',false, 'My new text');
}