我最近使用修改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