簡體   English   中英

TinyMCE驗證給出錯誤:無法調用未定義的方法“ getContent”

[英]TinyMCE validation gives error: Cannot call method 'getContent' of undefined

我有一個文本區域,其中的mce很小,我這樣加載它:

$(document).ready(function () {

    tinyMCE.init({
        mode: "textareas",
        ...

此文本區域為表格。 我將表單提交按鈕綁定到:

$('#btnSubmit').click(function() {

    tinymce.triggerSave();

    var editorContent = tinyMCE.get('Description').getContent();
    if (editorContent == '' || editorContent == null)
    {
        $(tinymce.activeEditor.getBody()).css("background-color", '#ffeeee');
        $(tinymce.activeEditor.getBody().parentNode).css("background-color", '#ffeeee');
        $(tinymce.activeEditor.getBody().parentNode).css("border", '1px solid #ff0000');
    }
});

在我的實體類中,我具有Required屬性。
我的目標是在模型無效時使tinyMCE背景變為red 但是我從題題中得到了錯誤。
有什么幫助嗎?
因此,驗證有效。 如果我刪除了textarea空支票,並保留了更改的顏色,它將更改。 但是問題是當文本區域中有東西時,我單擊“提交區域”,然后先變成紅色然后提交。
如果驗證失敗,也許有些功能可以使我做什么?

在我看來,這就像是未定義的對象錯誤-代碼無法解析此行tinyMCE.get('Description').getContent();

您似乎有時會在使用activeEditor之間混合使用, activeEditor有時則不使用,因此我對代碼進行了標准化,因此您始終依靠activeEditor這意味着我已刪除了觸發錯誤的行。 您似乎也可以在使用tinymcetinyMCE之間切換,這可能不會引起問題,但最好避免...因此我也對此進行了標准化。

沒有看到更多的方式來設置其余的代碼和標記,但是很難准確地知道發生了什么。 我的零錢能解決問題嗎?

$('#btnSubmit').click(function() {

  tinyMCE.triggerSave();

  var editorContent = tinyMCE.activeEditor.getContent();
  if (editorContent == '' || editorContent == null)
  {
    $(tinyMCE.activeEditor.getBody())
      .css("background-color", '#ffeeee')
      .parent()
      .css({
        "background-color": '#ffeeee',
        "border": '1px solid #ff0000'
      });
  }
});

如果您無法控制TinyMCE的初始化方法,則可以遵循此解決方案。

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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM