【问题标题】:How can get the parent iFrame ID when blur in tinymce editor?在tinymce编辑器中模糊时如何获取父iFrame ID?
【发布时间】:2019-08-14 15:51:34
【问题描述】:

我在一页上实现了 4 个 TinyMCE 编辑器。我想在用户离开 TinyMCE 编辑器并将编辑器的 html 放入 textarea 时获取编辑器 ID。在模糊时,我可以获得编辑器 html。但我在 Firefox 和 IE 中找不到 iFrame ID。我试过这段代码。

tinyMCE.init({
    mode : "textareas",
    theme : "advanced",
    plugins : "table,insertdatetime,fullscreen,searchreplace,emotions,paste,",
    selector: "#vereinbarungen",
    selector: "#narration",
    theme_advanced_buttons1 : "insertdate,inserttime,|,hr,emotions,|,search,replace,|,cut,copy,paste,pastetext,pasteword,|,forecolor,backcolor,|,bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist,undo,redo,|,fullscreen", 
    theme_advanced_buttons2 : "",
    theme_advanced_buttons3 : "",
    theme_advanced_toolbar_location : "bottom",
    theme_advanced_toolbar_align : "left",
    extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
    plugin_insertdate_dateFormat : "%d.%m.%Y",
    plugin_insertdate_timeFormat : "%H:%M:%S",
    setup : function(ed) {    
        ed.onInit.add(function(ed, event) {
            var dom = ed.dom,
            doc = ed.getDoc(),
            el = doc.content_editable ? ed.getBody() : (tinymce.isGecko ? doc : ed.getWin());
            tinymce.dom.Event.add(el, 'blur', function(e) {

                //this is the targeted iframe id this works in chrome but not works in other browsers.

                target_id = el.frameElement.id;
                html = $(ed.getBody()).html();
            });
        });
    },

});

当我用 Chrome 尝试这段代码时,我得到了target_id,但是当我用其他浏览器尝试时,el.frameElement 是未定义的。

这个问题的解决方法是什么?

【问题讨论】:

    标签: javascript jquery tinymce


    【解决方案1】:

    如果我理解您的问题,当编辑器模糊时您想要做的是用编辑器的当前值更新底层<textarea>

    TinyMCE 中的triggerSave() API 将自动执行您想要的操作,而无需执行您当前正在执行的任何手动 JavaScript 工作。

    这里是例子:

    关键代码是这样的:

    setup: function (editor) {
        editor.on('blur', function (e) {
            console.log('Triggering a Save');
            tinymce.triggerSave();
            console.log(document.getElementById('content').value);
        });
    }
    

    triggerSave() 调用完成后,我使用JavaScript 显示<textarea> 中的内容,但除了调试之外,您不需要这些console.log() 调用。

    【讨论】:

    • 我使用的是旧版本的 TinyMCE,当我尝试 tinymce.triggerSave() 函数时,我收到一条错误消息 Uncaught TypeError: tinymce.triggerSave is not a function。由于其他一些依赖项,我无法升级 TinyMCE。旧版 TinyMCE 有解决办法吗?
    • 您使用的 TinyMCE 的确切版本是什么?这将是包含在您的帖子中的有用信息,因为这对于了解人们可以提出的建议很重要。
    • 我使用tinymce.EditorManager.activeEditor.editorId获取编辑器ID
    【解决方案2】:

    我检查 tinymce 的所有对象及其值,并通过

    获得活动编辑器 ID

    tinymce.EditorManager.activeEditor.editorId;

    这将返回活动编辑器 ID

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-19
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多