【问题标题】:How To insert an image at cursor position in tinymce如何在tinymce中的光标位置插入图像
【发布时间】:2011-07-08 16:58:11
【问题描述】:

我正在使用以下代码来获取tinymce的内容。

tinymce_content=tinyMCE.get('txt_area_id').getContent();
updated_content=tinymce_content+"<img src="sample.jpg">";

通过使用上述代码,我将图像插入到 tinymce 内容中。

问题:如何在tinymce内的光标位置插入图像(即)如何预测光标位置并分割内容以在获取的内容之间插入图像。

提前致谢

【问题讨论】:

    标签: javascript jquery tinymce


    【解决方案1】:

    @Thariama's answer 在 Chrome 中对我来说工作得很好,但在 IE 中却不行。我必须将其修改为以下内容才能使其在两种浏览器中都能正常工作:

    var ed = tinyMCE.get('txt_area_id');                // get editor instance
    var newNode = ed.getDoc().createElement ( "img" );  // create img node
    newNode.src="sample.jpg";                           // add src attribute
    ed.execCommand('mceInsertContent', false, newNode.outerHTML)
    

    (IE 告诉我 range 没有 insertNode 方法。)

    【讨论】:

    • 两种解决方案都不适合我。我的 IE 说:“SCRIPT16389:Erro não especificado。”这意味着“错误未用英文指定...使用 IE9 和 tiny 3.4
    • 我正在使用 tinymce 4.0 。它在 IE10 中运行良好,但在 IE11 SCRIPT16389 中出现以下错误:功能不正确。文件:tinymce.min.js,行:8,列:20915 你能帮帮我吗?
    【解决方案2】:

    这将在tinymce编辑器中的选定点(光标位置)插入一个图像节点

    var ed = tinyMCE.get('txt_area_id');                // get editor instance
    var range = ed.selection.getRng();                  // get range
    var newNode = ed.getDoc().createElement ( "img" );  // create img node
    newNode.src="sample.jpg";                           // add src attribute
    range.insertNode(newNode);                          // insert Node
    

    【讨论】:

    • 太好了,每当我在 tinymce 中感到挣扎时,我记得 thariama 谢谢你
    • 很高兴能够提供帮助
    • 只是为了让这篇已经很棒的帖子更加清晰......要刷新图像并确保它正确呈现,您必须确保在 tinyMCE 的 DOM 中创建元素而不是在窗口 DOM 中。因此,如果您使用createElement,请确保您执行opener.tinyMCE.selectedInstance.getDoc().createElement 之类的操作。
    • 和 ed.getDoc().createElement 完全一样
    • 嗨,伙计,我添加了一个与您的答案相关的问题。你能给我cmets吗?这是问题。 How to add style attribute to an image added with selection.getRng(); in TinyMCE
    猜你喜欢
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 2020-08-17
    相关资源
    最近更新 更多