【问题标题】:tinymce - Is it possible to call a custom plugin function from outside the toolbar?tinymce - 是否可以从工具栏外部调用自定义插件功能?
【发布时间】:2014-05-25 09:57:57
【问题描述】:

好的。我知道之前可能有人问过这个问题:

Here

但我的问题是我如何从外部按钮而不是工具栏按钮调用插件函数。

我添加了一个自定义插件:

tinymce.PluginManager.add('example', function(e) {
        function customfunction(){
                    e.focus(true);
                    alert('Hello TinyMce');
            }
        }
);

Check this on Fiddle

我从其他函数调用这个customfunction,当我点击Custom Button 时调用该函数。 像这样:

function clickme()
{
   tinymce.get('textareaid').plugins.example.customfunction();

}

按钮:

<button onclick="clickme()" >Custom Button</button>

但这对我不起作用?

通过这种方式调用custom plugin function,我做对了吗?

我错过了什么吗?

【问题讨论】:

    标签: javascript tinymce-4


    【解决方案1】:

    一种可能性是向工具栏添加一个带有唯一ID 的按钮并调用该按钮的单击事件。插件看起来像这样:

    tinymce.PluginManager.add('example', function(e) {
    
            function customfunction() {
                        e.focus(true);
                        alert('Hello TinyMce');
                }
    
    
            e.addButton('testButton', {
                id: "testButton",
                text: 'Example',
                icon: false,
                onclick: function() {
                        // calls the custom function
                        customfunction();
                    }
                });
        }
    );
    

    然后像这样初始化tinymce编辑器:

    tinymce.init({
        selector: "textarea",
        plugins: "example",
        // show the button
        toolbar: "testButton undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
    });
    

    最后调用按钮点击事件:

    function clickme()
    {
       document.getElementById("testButton").click();
    }
    

    不要使用add_filter。完整的代码构成您的 tinymce 小提琴:

    <script type="text/javascript">
    tinymce.PluginManager.add('example', function(e) {
            function customfunction() {
                        e.focus(true);
                        alert('Hello TinyMce');
                }
    
    
        e.addButton('testButton', {
            id: "testButton",
            text: 'Example',
            icon: false,
            onclick: function() {
                    customfunction();
                }
            });
    }
    );
    
    tinymce.init({
        selector: "textarea",
        plugins: "example",
        toolbar: "testButton undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
    });
    //add_filter('mce_external_plugins', 'example');
    function clickme()
    {
       document.getElementById("testButton").click();
    }
    
    </script>
    
    <form method="post" action="">
        <textarea name="content" id="textareaid"></textarea>
    </form>
    
    <button onclick="clickme();" >abc</button>
    

    【讨论】:

      【解决方案2】:

      你的函数存储在

      tinymce.PluginManager.items[0]
      

      检查 dom 你可以看到那个函数。尝试提醒它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-12-23
        • 1970-01-01
        • 1970-01-01
        • 2020-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多