Title: Write the following page
When the user taps the statistics button, the most frequently occurring character in the text box pops up in the window and displays the number of occurrences
Click the Statistics button when the effect:
Implementation code:<!DOCTYPE HTML> <HTMLxmlns= "http://www.w3.org/1999/xhtml"> <Head> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> <title></title> <Scripttype= "Text/javascript">
= function () { // Get Form object var form = document.getElementById ( " myform "); // register a Click event for the statistics button Form.btn.onclick = function () { // Gets the value in the text box var txt = Form.name.value; // Define an empty JS object to hold all characters and occurrences of the text in the air var JSON = {}; // Traversing the value in the text box is the string saved in the TXT variable for ( var I = 0 ; I < txt.length; I ++) { // Extract the sub-characters of the TXT string labeled I and determine if the JSON object is not defined as a property named character property if ( Json[txt.charat (i)]) { // No definition defines the property (representing the character that appears in the text box) and assigns a value of 1, representing the number of occurrences of the character in the text box for that property Json[txt.charat (i)] = 1 ; } Else { // already defined adds 1 to the value of the property, that is, the number of occurrences of the character representing the property in the text box plus 1 Json[txt.charat (i)]+ + ; } } //define two variables varMaxnum= 0;//The number of occurrences of the most frequently occurring characters in the text box defaults to 0 var Char = "";//represents the most frequently occurring word in a text box defaults think empty //traversing the property values of a JSON object for (varIinch json) { if (json[i]> maxnum) { // If the value of the JSON property is greater than Maxnum (that is, the character that corresponds to the property is asked to appear in this box more often than all previous characters), assign it to Maxnum and assign its property name to Char maxnum = Json[i]; Char = i;}} Alert ("The maximum number of characters that appear in the text box is:" + char + " occurs: " +maxnum); } }; </Script> </Head> <Body> <formID= "MyForm"> <inputtype= "text"name= "Name"value= "123" /> <inputtype= "button"name= "BTN"value= "Statistics"/> </form> </Body> </HTML>The main points of knowledge
JS Object attributes can be added later features, object properties of the traversal and other JS objects integrated application. JS related knowledge can refer to my previous blog
related actions for JavaScript objects
JavaScript Step-by-step exercises