execute js function after tinymce is loaded
我选择onchange触发js功能来显示/隐藏tinymce编辑器:
1 2 3 | <select id="mySelect" onChange="myFuntion()"> <option value="1">Yes</option> <option value="0">No</option> |
然后,我有一个带有tinymce的textarea(在页面加载时加载)。
1 2 | <textarea class="mce" id="myTextarea"></textarea> <script src="tinymce.js> // file with global tinymce.init({ ... }); |
js函数类似于:
1 2 3 4 5 | function myFuntion(){ if( $( '#mySelect' ).val() == '1' ) { tinymce.get( 'myTextarea' ).show(); } else { tinymce.get( 'myTextarea' ).hide(); } } $( document ).ready(function() { myFuntion(); }); // show/hide tinymce based on how the mySelect setting is on page load |
除了" $(document).ready(function(){myFuntion();});"之外,其他所有东西都很好用。 抛出错误"未捕获的TypeError:无法读取null的属性'show'",我认为是因为尚未加载tinymce。
有一种方法可以通过"何时加载tinymce>执行myFunction()"来更改"文档就绪功能"
PS:我使用tinymce 4,tinymce.init()在外部文件上,并且在其他页面上使用,所以我不希望编辑此文件
编辑:
我的实际解决方法是使用:
setTimeout(function(){myFunction();},1500);
但是如果有回调或类似功能,例如$(document).on('tinymce:init')会很棒
要增加Kim Gysen编写的答案,您可以始终使用JavaScript逐页修改/扩展您的标准初始化。
例如,从您的标准配置开始:
1 2 3 4 | baseConfig = { selector: 'textarea' .... } |
...由于这只是一个简单的JavaScript对象,因此您可以在使用该对象初始化TinyMCE之前向该对象注入其他属性/方法。
例如:
1 2 3 4 5 6 7 | customConfig = { setup: function (editor) { editor.on('init', function () { //Do what you need to do once TinyMCE is initialized }); } } |
然后,您可以将
1 | $.extend(baseConfig, customConfig); |
...这将采用
1 | tinymce.init(baseConfig); |
这种通用技术使您可以创建具有所有所需基本功能的"标准"配置,同时逐页注入其他配置选项。
因此,现在您可以在需要时触发TinyMCE中的" onInit"功能,而无需修改其他地方使用的核心配置。
在tinymce 4中尝试使用Promise
1 2 3 4 5 | tinymce.init({ //some settings }).then(function(editors) { //what to do after editors init }); |
根据文档,您必须声明回调,如下所示:
1 2 3 4 5 6 7 8 | function myCustomOnInit() { alert("We are ready to rumble!!"); } tinyMCE.init({ ... oninit : myCustomOnInit }); |
这是他们提供的用于确保tinyMCE准备就绪的句柄。
您在这里说的是:
I use tinymce 4, the tinymce.init() is on a external files and used on
other page, so i prefer not to edit this file
对我来说没有多大意义。
根据文档:
oninit: This option enables you to specify a function to be executed
when all editor instances have finished their initialization. This is
much like the onload event of an HTML page.
非常清楚地表明这是您应该采取的方法。
声明