简体   繁体   English

TinyMCE验证给出错误:无法调用未定义的方法“ getContent”

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

I have a text area with tiny mce, I load it like this: 我有一个文本区域,其中的mce很小,我这样加载它:

$(document).ready(function () {

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

This text area is in a form. 此文本区域为表格。 I bind forms submit button to: 我将表单提交按钮绑定到:

$('#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');
    }
});

In my entity class I have Required attribute. 在我的实体类中,我具有Required属性。
My goal is to make tinyMCE background red when model is not valid. 我的目标是在模型无效时使tinyMCE背景变为red But I get error from ti question title. 但是我从题题中得到了错误。
Any help? 有什么帮助吗?
So, validation works. 因此,验证有效。 If I remove textarea empty check and leave color changing it changes. 如果我删除了textarea空支票,并保留了更改的颜色,它将更改。 But the problem is when there is something in text area and I click submit area first become red and then submit. 但是问题是当文本区域中有东西时,我单击“提交区域”,然后先变成红色然后提交。
Is there maybe some fubction where I can do something if validation fail? 如果验证失败,也许有些功能可以使我做什么?

It sounds to me just like an undefined object error - where the code can't resolve this line tinyMCE.get('Description').getContent(); 在我看来,这就像是未定义的对象错误-代码无法解析此行tinyMCE.get('Description').getContent(); .

You seem to be mixing between using activeEditor sometimes and other times not, so instead I've standardised the code so you are always relying on activeEditor - this means I've removed the line that was triggering the error. 您似乎有时会在使用activeEditor之间混合使用, activeEditor有时则不使用,因此我对代码进行了标准化,因此您始终依靠activeEditor这意味着我已删除了触发错误的行。 You also seem to switch between using tinymce and tinyMCE which might not be causing problems but is best to be avoided... so I've standardised that as well. 您似乎也可以在使用tinymcetinyMCE之间切换,这可能不会引起问题,但最好避免...因此我也对此进行了标准化。

Without seeing more of the way the rest of the code and markup is set-up however it's a bit difficult to tell exactly what is going on. 没有看到更多的方式来设置其余的代码和标记,但是很难准确地知道发生了什么。 Does my change repair the problem? 我的零钱能解决问题吗?

$('#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'
      });
  }
});

If you do not have control over init method of TinyMCE then, you can follow this solution. 如果您无法控制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" ) );
    });
});

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM