如何检查CKEditor中是否有文本? [英] How to check whether CKEditor has some text in it?

查看:49
本文介绍了如何检查CKEditor中是否有文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些字段的HTML表单.其中之一是由CKEditor管理的文本区域.

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 in all the fields.

我知道如何检查CKEditor控件中是否包含任何内容,但是它可能是空" HTML标记,其中没有任何文本.

I know how I can check if CKEditor control has anything in it, but it could be "empty" HTML tags without any text in it.

如何检查文字?

服务器端,我正在使用类似PHP的trim(strip_tags($ content)),因此我想在JavaScript中使用相同的东西.

Server side I'm using something like PHP's trim(strip_tags($content)), so I'd like to have the same in JavaScript.

使用jQuery的解决方案也可以使用.

A solution using jQuery would also be usable.

推荐答案

这将起作用:

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

它将仅包含文本,并且不包含任何html标记.

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

更新

它肯定可以在 CKEditor演示页面上使用.使用Firefox和Firebug,转到Firebug控制台,然后输入:

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();

控制台将在编辑器中打印出没有html标记的文本.确保您的特定应用程序中的选择器正确.您可以像这样测试选择器:

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;

那应该等于1.如果为0,则说明您的选择器是错误的.

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

更新2

同样,我的代码仍然正确,并且仍然可以在该页面上使用.您只需要正确的选择器.在您链接到的页面上,它是ID为 cke_editor1 < span> .该特定页面未使用jQuery,因此需要一些额外的工作来证明此示例有效.安装 FireQuery ,"jqueryify"页面,然后在Firebug控制台中执行此操作(注意,您必须使用 jQuery 而不是 $ .FireQuery就是这样工作的.)

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 <span> 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();

简而言之,请确保您有正确的选择器进入iframe .从< div> 还是< textarea> 创建CKEditor并不重要.只要您可以选择CKEditor注入到DOM中的< iframe> ,就可以使用 .contents().find("body").text()获取该iframe的文本.您是否测试过jquery选择器以查看 .length == 1 ?

In short, make sure you have the right selector to get to your iframe. Whether you create your CKEditor from a <div> or a <textarea> is not important. As long as you can select the <iframe> that CKEditor injects into the DOM, you can use .contents().find("body").text() to get the text of that iframe. Have you tested your jquery selector to see if .length == 1?

这篇关于如何检查CKEditor中是否有文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆