我最近使用修改JavaScript中的字数统计方法为我的网站,使其在计算文本域的INTIAL量的话,但它并不完全工作字计数方法
function wordCounter(field,countfield)
{
var maxlimit = 200;
var wordcounter = maxlimit - information.value.split(' ').length;
for (x = 0; x < field.value.length; x++)
{
if(field.value.charAt(x) == " " && field.value.charAt(x-1) != " ") // Counts the spaces while ignoring double spaces, usually one in between each word.
{
wordcounter++
}
if (wordcounter > 250)
{
field.value = field.value.substring(0, x);
}
else
{
countfield.value = maxlimit - wordcounter;
}
}
}
由于某些奇怪的原因,无论我使用什么解决方案,它都会在计数中添加两个而不是一个 – 2009-11-17 19:51:42
微小问题:如果输入用空白字符左填充,这会夸大字数。 – richardtallent 2009-11-17 21:27:06
richardtallent - 感谢您的评论,正确的,添加内联修剪来解决。 – Jaanus 2009-11-18 16:41:15