【问题标题】:Event callback not getting tinymce target information事件回调未获取 tinymce 目标信息
【发布时间】:2015-12-22 21:00:12
【问题描述】:

我在一个页面上有两个tinymce 实例,当它们被更改时,我想更新一个最终保存到localStorage 的Javascript 对象。

当页面加载时,我从 localstorage 填充两个 tinymce 实例没有问题,但是当它们被更改时,我似乎无法传递适当的 jQuery event 其中包括 @ 987654327@ 信息发送给我的update 处理程序。我怀疑这是因为iframe,但我很难过。

在我的update 回调中,我需要知道e.targete.target.id,但它传递了一个不包含该信息的对象,如下所示:

{isTrusted: true, screenX: 953, screenY: 790, clientX: 298, clientY: 20…}
tinymce.init({             
         setup:function(ed){
              ed.on("init",function(e){
                  switch(e.target.id){                             
                      case "intro":
                          _content = obj.INFO.INTRO.TEXT;
                          break;
                      case "conclude":
                          _content = obj.INFO.CONCLUDE.TEXT;
                          break;
                      }
                      tinyMCE.activeEditor.setContent(_content);
         });
         ed.on("change",function(e){
              //update(jQuery.Event("edit",{target:$(e.currentTarget).find("body")}));
              //jQuery.Event("edit",{target:e.currentTarget})
              update(e);
         });
         ed.on("keyup",function(e){
              //update(jQuery.Event("edit",{target:$(e.currentTarget).find("body")}));
              update(e);
         });
         ed.on("click",function(e){
              //update(jQuery.Event("edit",{target:$(e.currentTarget).find("body")}));
              //jQuery.Event("edit",{target:tinyMCE.activeEditor, editor:tinyMCE.activeEditor.id})
              update(e);
         });
    }
});

【问题讨论】:

    标签: javascript jquery iframe tinymce jquery-events


    【解决方案1】:

    这里的问题是你要获取编辑器ID。您可以使用以下方法获取它:

    setup:function(ed){
         ed.on("init",function(e){
                   console.log('target id:',e.target.id);
         });
         ed.on("click",function(e,f){
           var editor_id = e.view.frameElement.id.slice(0, -4);
           console.log('editor_id:', editor_id);
    
           // or much easier using ed.id
           editor_id = ed.id;
    
           // if you want to address the editor body you can use ed.getBody()
    
           var my_editor = tinymce.get(editor_id);
           // update(e);
         });
    },
    

    查看我创建的 tinymce fiddle 的实时示例:http://fiddle.tinymce.com/6mfaab/1

    【讨论】:

    • 谢谢塔里亚马。这使我走上了正确的道路。我最终不得不这样做: update(jQuery.Event("mce_edit",{target:$(e.view.frameElement.closest("section"))[0]}));
    • 很高兴能够提供帮助
    猜你喜欢
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多