5

私が取り組んでいるプロジェクトでTinymce(jQueryを使用)を使用しています。ユーザーが情報を入力するためにリッチテキストエディターを使用します。ただし、ページをロードするときにFirefoxとChromeが「tinymceis not defined」エラーを検出する場合もありますが(コードの異なる行で)、ページが正常にロードされる場合もあります。奇妙なのは、IEで完全に機能することです。

これが私が使用しているコードのビットです:

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

そして後で

_variable.find("#summary").tinymce().setContent(content);

この行は、エラーが(時々)キャッチされる場所です。tinyMCEプラグインはこの行の約5000行前に初期化されていますが、問題は読み込みの問題であるように思われます。

更新:今のところ、setTimeoutで問題を「解決」することができましたが、これは本当に醜い方法のようです。

4

2 に答える 2

6

いくつかのポイント:

  • readyTinyMCEの初期化がjQueryイベント関数内で行われるかどうかについては言及していません。もちろんそうあるべきです。

  • 各ループは必要ありません。あなたはただ言うことができます:

$('textarea.rich-text').tinymce({ script_url : '../js/tinymce/jscripts/tiny_mce/tiny_mce.js', theme : "advanced", ... });

  • findIDで選択しているだけなので、への呼び出しは必要ありません。ただ行う:

$("#summary").tinymce().setContent(content);

  • あなたの本当の問題はおそらく、エラーが発生したときに、tinymceがそれ自体の初期化を完了していないことです。構成済みのからスクリプトをロードする必要があることがわかりますscript_url。しばらく時間がかかる場合があります。したがって、oninitなどのコールバックを使用する必要があります。
于 2012-07-27T00:20:01.947 に答える
0

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" ) );
    });
});

参照:http://blog.incognitech.in/tinymce-undefined-issue/

編集:ソリューションが含まれています

于 2015-01-28T10:42:58.880 に答える