单词及字符的统计脚本
要完成此效果需要两个步骤第一步:把如下代码加入到<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
- 联系我们: QQ:82526114(技术) 411523648(客服) 237057746(财务)
- 电话:+86-762-4372098 邮箱:webmaster@814e.net,support@814e.net
- 粤ICP备05002242号 网监局备案:4403701910502
