我的博客| Blog
- ·微信小程序错误:VM564:...
- ·mongodb3.2设置密码...
- ·单行滚动代码-单行滚动效果
- ·自己动手制作图形字体,以便于...
- ·MySQL无限级分类PHP按...
- ·Windows下的Apach...
- ·如何将网站上的文章分享到微信...
- ·PHP实现自动获取本月第几个...
联系我| Contact Me
- 电话:18681257080
- QQ:271538869
- 邮编:518020
- 信箱:service@08321.org
- 地址:四川省内江市资中县
诚信稳健,和谐共赢
- 以诚信为立世之本,在稳健的基础上,不断寻求创新与突破。
- 以务实严谨、精微细致的专业精神,为客户做最优质的策划,实现效果最大化。
成功,依稀可见
- 成功,依稀可见!——依希设计
- 成功是很多方面的,很多小的成功可以积累成大的成功,而真正意义上的成功是永远不可能到达的,所以只能依稀可见。
我的博客
FCKeditor字数统计,FCKeditor编辑器字数,怎么用JS统计content字数
来源:本站编辑 发布日期:2011-6-10 已有 人浏览过此信息
做大型网站的时候,众所周知,数据库里面的字段都是有长度限制的,哪怕是 LongText、Text 这些类型也有字数上限。但用户并不知情,提交的内容可谓五花八门,字数很多。提交上去导致数据库不能容纳,返回又会清空数据。
如果能让客户端JS来完成这个工作当然最好啦,怎么能让用户在客户端就知道自己的字数,并适当限制字数的提交呢?
我们以大名鼎鼎的 FCKeditor 为例来看看:
为了解决 FCKeditor字数统计,FCKeditor编辑器字数,怎么用JS统计content字数 问题我们需要用到 FCKeditor 的API,获取到 FCKeditor编辑器中已输入的字数,然后用JS进行判断是否超过设定值,代码如下:
<script type="text/javascript">
<!--
function checkform(){
var FCKConfig=document.getElementById("content");var str=FCKeditorAPI.GetInstance('content').GetXHTML(FCKConfig.FormatOutput);
if (str.length>20000){alert('字数过多,请控制在20000个字符以内');return false;}
return true;
}
//-->
</script>
<form action="" method="post" name="myform" onsubmit="return checkform();">
<textarea name="content" id="content" style="display:none;"/></textarea>
<iframe id="FCK_Frame" src="../../fckeditor/editor/fckeditor.html?InstanceName=content&Toolbar=Basic" width="100%" height="450" frameborder="no" scrolling="no"></iframe>
</form>
当FCKeditor编辑器中的字数超过 20000字(这里未进行清除HTML标签操作)时,显示提示,并取消表单提交。
至此,FCKeditor字数统计,FCKeditor编辑器字数,怎么用JS统计content字数 的问题已经解决。
注: 如果不使用编辑器,直接使用文本域的话,那代码就要简单多啦。使用 textarea的 outerText 属性即可,如:
<script type="text/javascript">
<!--
function checkform(){
var obj=document.all.myform;
if (obj.content.outerText.length>20000){alert(''字数过多,请控制在20000个字符以内');return false;}
return true;
}
//-->
</script>
<form action="" method="post" name="myform" onsubmit="return checkform();">
<textarea name="content"/></textarea>
</form>
如果能让客户端JS来完成这个工作当然最好啦,怎么能让用户在客户端就知道自己的字数,并适当限制字数的提交呢?
我们以大名鼎鼎的 FCKeditor 为例来看看:
为了解决 FCKeditor字数统计,FCKeditor编辑器字数,怎么用JS统计content字数 问题我们需要用到 FCKeditor 的API,获取到 FCKeditor编辑器中已输入的字数,然后用JS进行判断是否超过设定值,代码如下:
<script type="text/javascript">
<!--
function checkform(){
var FCKConfig=document.getElementById("content");var str=FCKeditorAPI.GetInstance('content').GetXHTML(FCKConfig.FormatOutput);
if (str.length>20000){alert('字数过多,请控制在20000个字符以内');return false;}
return true;
}
//-->
</script>
<form action="" method="post" name="myform" onsubmit="return checkform();">
<textarea name="content" id="content" style="display:none;"/></textarea>
<iframe id="FCK_Frame" src="../../fckeditor/editor/fckeditor.html?InstanceName=content&Toolbar=Basic" width="100%" height="450" frameborder="no" scrolling="no"></iframe>
</form>
当FCKeditor编辑器中的字数超过 20000字(这里未进行清除HTML标签操作)时,显示提示,并取消表单提交。
至此,FCKeditor字数统计,FCKeditor编辑器字数,怎么用JS统计content字数 的问题已经解决。
注: 如果不使用编辑器,直接使用文本域的话,那代码就要简单多啦。使用 textarea的 outerText 属性即可,如:
<script type="text/javascript">
<!--
function checkform(){
var obj=document.all.myform;
if (obj.content.outerText.length>20000){alert(''字数过多,请控制在20000个字符以内');return false;}
return true;
}
//-->
</script>
<form action="" method="post" name="myform" onsubmit="return checkform();">
<textarea name="content"/></textarea>
</form>
