How to check whether CKEditor has some text in it?

后端 未结 6 1550
时光取名叫无心
时光取名叫无心 2021-02-04 02:08

I have an HTML form with a few fields. One of them is a textarea managed by CKEditor.

When the user wants to submit the form, I want to check whether he entered values

6条回答
  •  你的背包
    2021-02-04 02:31

    This will work:

    $("#editorContainer iframe").contents().find("body").text();
    

    That will contain only the text, and none of the html tags.

    UPDATE

    It definitely works on the CKEditor demo page. Use Firefox and Firebug, go to the Firebug console, and type in:

    $("#demoInside iframe").contents().find("body").text();
    

    The console will print out the text in the editor, with no html tags. Make sure you have the selector correct in your particular application. You can test your selector like this:

    $("#demoInside iframe").contents().find("body").length;
    

    That should equal 1. If it's 0, your selector is wrong.

    UPDATE 2

    Again, my code is still correct, and it still works on that page. You just need the right selector. On the page you linked to, it is a with id cke_editor1. That particular page does not make use of jQuery, so it requires some additional work to prove this sample works. Install FireQuery, "jqueryify" the page, and then do this in the Firebug console (note you have to use jQuery and not $. That's how FireQuery works).

    jQuery("#cke_editor1 iframe").contents().find("body").text();
    

    In short, make sure you have the right selector to get to your iframe. Whether you create your CKEditor from a

    or a