Add a button after loading TinyMCE via JQuery and it's not working

一曲冷凌霜 提交于 2019-12-13 20:14:31

问题


I'm loading TinyMCE via JQuery and after it's loaded, I'd like to add a save button to it. The save button is calling a function but Firebug says the function is not defined, in this case destroyTinyMCE() is not defined. What's wrong?

$('div#introText').click(function() {
        loadTinyMCE();
        $('div#introText').after('<input value="Save" onclick="destroyTinyMCE();" type="button">');
});

function loadTinyMCE() {
//some variable
}

function destroyTinyMCE() {
       $('div#introText').tinymce().destroy();
       $('div#introText').tinymce().remove();
}

回答1:


If this is inside your document.ready handler, then that destroyTinyMCE function is scoped only to it, and when looking for it in the global namespace (as onclick="destroyTinyMCE();" will do), it won't be there. Instead attach the click handler when creating it, like this:

$('<input value="Save" type="button">').click(destroyTinyMCE)
                                       .insertAfter('div#introText');

This will reference the function correctly and it can still be tucked away inside whatever closure you're in currently.



来源:https://stackoverflow.com/questions/3486352/add-a-button-after-loading-tinymce-via-jquery-and-its-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!