首页 >> 网页特效 >> 计数转换 >> 正文
来源: 不详 | 时间: 2006-1-15 19:10:41 | 人气:
单词及字符的统计脚本
要完成此效果需要两个步骤
第一步:把如下代码加入到<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>