我在编辑器中使用 FCK 编辑器和字符长度 <= 2000(这是可配置的),包括格式(即它生成的 HTML 标记)。如果用户尝试输入超过 2000 个字符,我会提示“您不能输入超过 2000 个字符”。但是,如果用户输入 1999 个字符并单击格式化工具栏图标,则超过 2000 个字符。所以我处理了这个并修剪了额外的字符。在这个功能中,我面临着问题。如果某些格式位于编辑器的末尾,则在修剪 HTML 结束标记时会被 editorInstance.SetHTML(text.substr(0,charLimit)); 功能。所以所有的 HTML 标签在编辑器中都是可见的请帮助。
问问题
672 次
1 回答
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);
}
}
}
于 2010-01-22T11:51:20.803 回答