TinyMCE:初始化后如何绑定事件 [英] TinyMCE: How bind on event after its initialized

查看:2092
本文介绍了TinyMCE:初始化后如何绑定事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了很多但是通过 google-fu 来获取任何结果:(

I already searched a lot but by google-fu'ing don't get me any results :(

我已经有了初始化 tinyMCE 编辑器我无法控制哪个初始化过程,所以下面的代码根本不起作用:

I have an already initialized tinyMCE editor which initialization process I cannot control, so code like the following don't work at all:

tinyMCE.init({
   ...
   setup : function(ed) {
          ed.onChange.add(function(ed, l) {
                  console.debug('Editor contents was modified. Contents: ' + l.content);
          });
   }
});

由于未定义jQuery tinymce插件,因此以下代码无效:

Code like the following doesn't work either since the jQuery tinymce plugin isn't defined:

$('textarea').tinymce({
    setup: function(ed) {
        ed.onClick.add(function(ed, e) {
            alert('Editor was clicked: ' + e.target.nodeName);
        });
    }
});

我的意思是,它必须使用 tinymce.something 语法。

I mean, it has to be using the tinymce.something syntax.

如何在tinyMCE初始化之后将回调函数绑定到任何tinyMCE事件?

推荐答案

在使用console.log()侵入tinymce对象后,我找到了一个可行的解决方案:

After hacking into the tinymce object with console.log(), I found a working solution:

setTimeout(function () {
       for (var i = 0; i < tinymce.editors.length; i++) {
             tinymce.editors[i].onChange.add(function (ed, e) {
             // Update HTML view textarea (that is the one used to send the data to server).
         ed.save();
       });
            }
}, 1000);

在回调函数中,可以设置任何想要的事件绑定。

Inside that callback function one can set the whatever event binding one wants.

setTimeout 调用是为了克服 tinymce 和<$ c $的竞争条件c> jQuery ,因为当调用 tinymce.editors [i] .onChange.add()时,tinymce尚未初始化。

The setTimeout call is to overcome the racing condition of tinymce and jQuery, since when the call to tinymce.editors[i].onChange.add() is made tinymce wasn't initialized yet.

这篇关于TinyMCE:初始化后如何绑定事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆