Jquery plugins Tinymce(有时)是未定义的

Jquery plugins Tinymce(有时)是未定义的,jquery-plugins,tinymce,Jquery Plugins,Tinymce,我正在工作的项目中使用Tinymce(带jQuery);我们使用富文本编辑器供用户输入信息;但是,有时在加载页面时,Firefox和Chrome会检测到“tinymce未定义”错误(有时在不同的代码行),而其他时候页面会正常加载。奇怪的是,它与IE完美配合 下面是我正在使用的一些代码: view.find('textarea.rich-text').each(function () { $(this).tinymce( /* ...rules... */);

我正在工作的项目中使用Tinymce(带jQuery);我们使用富文本编辑器供用户输入信息;但是,有时在加载页面时,Firefox和Chrome会检测到“tinymce未定义”错误(有时在不同的代码行),而其他时候页面会正常加载。奇怪的是,它与IE完美配合

下面是我正在使用的一些代码:

view.find('textarea.rich-text').each(function () {        
   $(this).tinymce( /* ...rules... */);        
});  
后来

_variable.find("#summary").tinymce().setContent(content);
这一行是错误(有时)被捕获的地方。在我看来,这个问题是一个加载问题,即使tinyMCE插件在这行之前初始化了大约5000行

更新:目前,我已设法通过设置超时来“解决”问题,但这似乎是一种非常糟糕的方法。

有几点:

  • 您没有提到TinyMCE初始化是否在jQuery
    ready
    事件函数中完成。当然应该是这样

  • 您不需要每个循环都使用。你可以说:

$('textarea.richtext').tinymce({
script_url:“../js/tinymce/jsscripts/tiny_mce/tiny_mce.js”,
主题:“高级”,
...
}); 

  • 您不需要调用
    find
    ,因为您只是通过id进行选择。只需执行以下操作:
$(“#摘要”).tinymce().setContent(content)

  • 您真正的问题可能是tinymce在出现错误时尚未完成自身初始化。您可以看到,它必须从配置的
    脚本\u url
    加载脚本。这可能需要一段时间。因此,您必须使用回调,例如

如果您无法控制TinyMCE的init方法,则可以遵循此解决方案

jQuery(document).ready(function($) {

    function myCustomSetContent( id, content ) {
        // Check if TinyMCE is defined or not.
        if( typeof tinymce != "undefined" ) {
            var editor = tinymce.get( id );
            // Check if TinyMCE is initialized properly or not.
            if( editor && editor instanceof tinymce.Editor ) {
                editor.setContent( text );
                editor.save( { no_events: true } );
            } else {
                // Fallback
                // If TinyMCE is not initialized then directly set the value in textarea.
                //TinyMCE will take up this value when it gets initialized.
                jQuery( '#'+id ).val( text );
            }
            return true;
        }
        return false;
    }

    function myCustomGetContent( id ) {
        // Check if TinyMCE is defined or not.
        if( typeof tinymce != "undefined" ) {
            var editor = tinymce.get( id );
            // Check if TinyMCE is initialized properly or not.
            if( editor && editor instanceof tinymce.Editor ) {
                return editor.getContent();
            } else {
                // Fallback
                // If TinyMCE is not initialized then directly set the value in textarea.
                // TinyMCE will take up this value when it gets initialized.
                return jQuery( '#'+id ).val();
            }
        }
        return '';
    }

    $(".class-to-update-content").on("click", function(e) {
        myCustomSetContent( "tinymce-editor-id", "New Content in Editor" );
    });

    $(".class-to-get-content").on("click", function(e) {
        $("div.class-to-display-content").html( myCustomGetContent( "tinymce-editor-id" ) );
    });
});
参考:


编辑:包含解决方案

谢谢。我做了相应的更改,并确保在jquery之前初始化了tinymce(我在其他问题上看到这通常是个问题),但我仍然收到相同的错误:
TypeError:$(“#摘要”).tinymce()是未定义的
,所以您将该行放入了
oninit
回调?当我试图添加下一步应该调用的函数时,firebug说有一个“未定义的错误”;即使它们在同一个页面上,它也无法识别该方法,因此我将其删除。
$('textarea.richtext').tinymce({…oninit:function(){$(“#summary”).tinymce().setContent(content);})谢谢!成功了!我希望我能说这已经解决了,但现在唯一的问题是,第二个文本编辑器(名为“备份”)在我尝试执行相同操作时引发了一个错误,即:
$('textarea.rich text').tinymce({…oninit:function(){$(“#summary”).tinymce().setContent(content);$(“#备份”).tinymce().setContent(otherContent);})错误在第二行;很好。它是否需要自己的功能才能工作?