学习首页 百科 人生课堂 办公软件 英语学习 操作系统 故事会 编程资料 软件学习 设计
铭瑶网 >> 学习首页 >> 编程 >> 单词及字符的统计脚本
标题:单词及字符的统计脚本

【字体: 】 时间:2008-4-11 来源:互联网 作者:study

单词及字符的统计脚本

要完成此效果需要两个步骤第一步:把如下代码加入到<head>区域中<SCRIPT LANGUAGE="JavaScript"><!-- Beginfunction 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>第二步:把如下代码加入到<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> 

查看/参与:讨论/评论 相关文章:Javascript