setcontent将文本插入到IE文本区域的开头

tinymce.selection.setContent inserts the text at the begining of the textarea in IE

本文关键字:文本 区域 IE 开头 插入 setcontent      更新时间:2023-09-26

我为tinyMCE创建了一个插件,用于使用MathJax在编辑器中插入数学公式。这个插件在iframe中打开一个弹出窗口(使用jQuery),然后启动一个触发事件,将输入的公式插入到tinyMCE活动编辑器中。

我的代码在Chrome中正常工作&Firefox(创建一个pre,插入到文本区域的插入符位置),但在IE中,文本插入到文本区域的开头。

我使用setContent方法像这样:

tinyMCE.activeEditor.selection.setContent(text to insert, {format: 'bbcode'});

我尝试使用ed.focus()之前插入和其他建议在StackOverflow中找到,但没有为我工作。

另外,我试图在打开弹出框之前保存插入符号的位置,并在插入时恢复它,但无论如何都不起作用。

任何想法?

SOLVED:

我知道这不是最优雅的解决方案,但对我来说很有效。

在打开弹出窗口之前,我插入一个带有特定ID的"span",如下所示:

var sel = tinyMCE.activeEditor.selection;
sel.setContent('<span id="_math_marker">&nbsp;</span>');

然后,当弹出窗口关闭并将文本发送回编辑器时,我寻找带有标记的span,然后选择它并调用setContent:

var ed = tinyMCE.activeEditor;
var marker = ed.dom.get('_math_marker');
ed.selection.select(marker, false);
ed.selection.setContent("TEXT TO INSERT");

这适用于所有浏览器!请记住,如果弹出窗口关闭而不插入任何内容,请删除span,以避免在编辑器中留下垃圾。

: -)

只需删除。selection部分,因此您的代码应该看起来像

tinyMCE.activeEditor.setContent(text to insert, {format: 'bbcode'});

在IE8中无法激活编辑器,所以需要聚焦,所以使用

tinyMCE.activeEditor.focus ();

试试这样:

<>之前 var myField = tinyMCE.get("SMSBody"); //IE support if (document.selection) { myField.focus(); sel = document.selection.createRange(); sel.text = fieldName; } else if (document.getSelection) { tinyMCE.activeEditor.selection.setContent(fieldName); myField.focus(); } 之前

嗨,我使用了一个叫做jCaret的jQuery插件来处理这些情况。在大多数常用浏览器(ie7、ie8、ie9以及兼容模式)下都能完美运行。

请看下面链接中给出的例子

jCaret

谢谢