当前位置:开发者网络 >> 网页特效 >> 计数计算 >> 内容
精彩推荐
分类最新特效
分类热点特效
  
单词及字符的统计脚本
日期:2005-06-04   人气:   【字体: 】【收藏此文


<!-- 网页特效代码由[中国WEB开发者网络:http://www.ChinaWebDev.com]提供! -->
<!-- 要实现此效果需要 2 个步骤: -->

<!-- 第 1 步: -->
<!-- 把下面的代码加到<HEAD></HEAD>区域中: -->

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function CountWords (this_field, alertWords, alertChars) {
if (alertWords == null) {
alertWords = true;
}
if (alertChars == null) {
alertChars = false;
}
var fullStr = this_field.value;
var charCount = fullStr.length;
var rExp = /[^A-Za-z0-9]/gi;
var spacesStr = fullStr.replace(rExp, " ");
var cleanedStr = spacesStr + " ";
do {
var old_str = cleanedStr;
cleanedStr = cleanedStr.replace(" ", " ");
} while(old_str != cleanedStr);
var splitString = cleanedStr.split(" ");
var wordCount = splitString.length -1;
if (fullStr.length <1) {
wordCount = 0;
}
if (wordCount == 1) {
wordOrWords = " 个单词";
}
else {
wordOrWords = " 个单词";
}
if (charCount == 1) {
charOrChars = " 个字符";
} else {
charOrChars = " 个字符";
}
if (alertWords & alertChars) {
alert ("统计结果:\n" + " " + wordCount + wordOrWords + "\n" + " " + charCount + charOrChars);
}
else {
if (alertWords) {
alert ("Word Count: " + wordCount + wordOrWords);
}
else {
if (alertChars) {
alert ("Character Count: " + charCount + charOrChars);
}
}
}
return wordCount;
}
// End -->
</script>




<!-- 第 2 步: -->
<!-- 把下面的代码加到<BODY></BODY>区域中: -->

<form>
<textarea cols=40 rows=5 name=x>
</textarea>
<br>
<input type=button value="Count Words" OnClick ="CountWords(this.form.x, true, true);">
</form>