使用FCK编辑器&编辑器中的字符长度< = 2000(这是可配置的)包括格式化(即它生成的HTML标记)。如果用户试图输入超过2000个字符,我会保持警告“你不能输入超过2000个字符”。但如果用户输入1999个字符并单击格式工具栏图标,则超过2000个字符。所以我处理了这个并修剪了额外的字符。在这个功能中,我正面临着这个问题。如果某些格式设置在编辑器的末尾,则转发HTML结束标记会被截断 editorInstance.SetHTML(text.substr(0,charLimit)); 功能。所以所有的HTML标签都在编辑器中可见请帮忙。
答案 0 :(得分:0)
function IntervalClass()
{
var intervalID;
this.addSetInteval = function(val)
{
this.intervalID = val;
}
this.clearIntevals = function()
{
clearInterval(this.intervalID);
}
}
function addListners(editorName, charLimit, infoDiv)
{
if(editorName == null)
return;
var objInterval = new IntervalClass();
var editorInstance = FCKeditorAPI.GetInstance(editorName);
var oDOM = editorInstance.EditorDocument;
editorInstance.Events.AttachEvent( 'OnPaste', function(){ return TrimCharacters(editorInstance, charLimit, infoDiv, objInterval); }) ;
if (document.all)
{
oDOM.attachEvent("onkeypress",function(){ return LengthValidation(editorInstance, charLimit, infoDiv); });
oDOM.attachEvent("onkeyup",function(){ return TrimChars(editorName, charLimit, infoDiv, objInterval); });
}
else
{
oDOM.addEventListener("keypress",function(){ LengthValidation(editorInstance, charLimit, infoDiv); });
oDOM.addEventListener("keyup",function(){ TrimChars(editorName, charLimit, infoDiv, objInterval); });
}
objInterval.addSetInteval(setInterval(function(){ TrimChars(editorName, charLimit, infoDiv, objInterval); },1000));
}
function LengthValidation(editorInstance, charLimit, infodiv)
{
var text = editorInstance.GetHTML(true);
var textlength = text.length;
if(textlength > 0)
change();
if(textlength >= charLimit)
{
alert('You cannot enter more than (' + charLimit + ') characters!');
infodiv.innerHTML = 'You cannot enter more than (' + charLimit + ') characters!';
return false;
}
if(infodiv != null)
infodiv.innerHTML = '(' + (charLimit - textlength) + ') Characters Remaining';
return true;
}
function TrimCharacters(editorInstance, charLimit, infodiv, objInterval)
{
var text = editorInstance.GetHTML(true);
var textlength = text.length;
if(textlength > charLimit)
{
objInterval.clearIntevals();
editorInstance.SetHTML(text.substr(0,charLimit));
TrimXtraChars(editorInstance, charLimit, infodiv)
alert('You cannot enter more than (' + charLimit + ') characters!');
addListners(editorInstance.Name, charLimit, infodiv)
}
else
{
var textHtml = editorInstance.GetClipboardHTML();
if(textHtml.length + textlength <= charLimit)
{
editorInstance.InsertHtml( textHtml ); //.toLowerCase()
}
else
{
alert('Your paste action is resulting in more than (' + charLimit + ') characters!');
}
}
text = editorInstance.GetHTML(true);
textlength = text.length;
if(infodiv != null)
infodiv.innerHTML = '(' + (charLimit - textlength) + ') Characters Remaining';
return false;
}
function TrimChars(editorName, charLimit, infodiv, objInterval)
{
var editorInstance = FCKeditorAPI.GetInstance(editorName);
var text = editorInstance.GetHTML(true);
var textlength = text.length;
if(textlength > charLimit)
{
objInterval.clearIntevals();
editorInstance.SetHTML(text.substr(0,charLimit));
TrimXtraChars(editorName, charLimit, infodiv)
alert('You cannot enter more than (' + charLimit + ') characters!');
addListners(editorInstance.Name, charLimit, infodiv)
}
text = editorInstance.GetHTML(true);
textlength = text.length;
if(infodiv != null)
infodiv.innerHTML = '(' + (charLimit - textlength) + ') Characters Remaining';
}
function TrimXtraChars(editorName, charLimit, infodiv)
{
var editorInstance = FCKeditorAPI.GetInstance(editorName);
var text = editorInstance.GetHTML(true);
var txtlen = text.length;
if(txtlen <= charLimit)
return;
if(txtlen > charLimit)
{
var diffLen = (txtlen - charLimit);
var AlterText = text;
if(text.lastIndexOf("<") != -1)
AlterText = text.substr(0,text.lastIndexOf("<"));
if(AlterText.length > charLimit)
{
editorInstance.SetHTML(AlterText.substr(0,charLimit-(diffLen+diffLen)));
TrimChars(editorName, charLimit, infodiv);
}
else
{
editorInstance.SetHTML(AlterText);
}
}
}