A tool used to count the number of occurrences of each string in jQuery.

Source: Internet
Author: User

Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> tool: count the number of occurrences of each string in jQuery </title>
<Script src = "http://demo.jb51.net/jslib/jquery/jquery.js" type = "text/javascript"> </script>
</Head>
<Body>
<P> source file (copy jQuery source code to the following text field): </p>
<P> <textarea id = "myjquery" style = "width: 510px; height: 250px; padding: 2px;">... </textarea> </p>
<P> <input id = "mybtn2" style = "position: relative; left: 349px; "type =" button "value =" count the number of occurrences of all strings "/> </p>
<Div id = "myshow2" style = "width: 500px; height: 250px; border: 1px dotted #8B8D72; overflow: auto; padding: 5px;"> </div>
<P> </p>
<P> <label for = "myinput"> string: <input id = "myinput" type = "text"/> <input id = "mybtn1" style = "position: relative; left: 130px; "type =" button "value =" count the number of occurrences of a single string"
/> </Label> </p>
<Div id = "myshow1" style = "width: 500px; height: 100px; border: 1px dotted #8B8D72; overflow: auto; padding: 5px;"> </div>
<P> </p>
<P> after clicking the "count the number of occurrences of all characters" button, the string "string" appears 44 times, most of which are the following code </p>
<Div class = "cnblogs_Highlighter">
<Pre class = "brush: javascript; gutter: true;"> typeof selector = "string"
Typeof data! = "String"
Type = "string"
Typeof context = "string"
GetByName = typeof name = "string"
//...
</Pre>
</Div>
<P> </p>
<P> If a variable is replaced, the file size will be further reduced when the tool is compressed. </P>
<P> </p>
<P> </p>
<Script type = "text/javascript"> // <! [CDATA [
SetTimeout (function (){
Function builsid (keywords ){
Var rObj = {};

If (keywords. constructor! = Array ){
Return;
}

Keywords. forEach (function (it ){
RObj [it] = RegExp (''+ it, 'G ');
});

Return rObj;
}
Function count (rObj, source, callback, sortType ){
Var r,
Rarr,
Num,
Type,
Func,
Result = [];

Var subCount = function (arr ){
Var I, re, num, resu;
I = num = 0;

For (I; I <arr. length; I ++ ){
Re = arr [I];
While ((resu=re.exe c (source ))! = Null ){
Num ++;
}
}

Return num;
};

For (type in rObj ){
Rarr = rObj [type];

If (rarr. constructor! = Array ){
Rarr = [rarr];
}

Num = subCount (rarr );
Result. push ({type: type, num: num });
}

// Sort 0: Number Order 1: Number descending
If (typeof sortType! = 'Undefined '){
If (sortType = 0 ){
Func = function (a, B ){
Return a. num-B. num;
};
} Else if (sortType = 1 ){
Func = function (a, B ){
Return B. num-a. num;
};
}
Result. sort (func );
}

Callback (result );

}
Function main (keywords, source, callback, sortType ){
Var rObj = keywords. constructor = Array? Builsid (keywords): keywords;
Count (rObj, source, callback, sortType );
}
Var rObj = {
String: [/\ 'string \ '/g,/\ "string \"/g],
Number: [/\ 'Number \ '/g,/\ "number \"/g],
'Boolean': [/\ 'boolean \ '/g,/\ "boolean \"/g],
Object: [/\ 'object \ '/g,/\ "object \"/g],
'Undefined': [/\ 'undefined \ '/g,/\ "undefined \"/g],
'Function': [/\ 'function \ '/g,/\ "function \"/g],
'Array': [/\ 'array \ '/g,/\ "array \"/g],
'Ready': [/\ 'ready \ '/g,/\ "ready \"/g],
Input: [/\ 'input \ '/g,/\ "input \"/g],
Type: [/\ 'Type \ '/g,/\ "type \"/g],
Text: [/\ 'text \ '/g,/\ "text \"/g],
Radio: [/\ 'Radio \ '/g,/\ "radio \"/g],
Checkbox: [/\ 'checkbox \ '/g,/\ "checkbox \"/g],
Password: [/\ 'password \ '/g,/\ "password \"/g],
Submit: [/\ 'submit \ '/g,/\ "submit \"/g],
Button: [/\ 'button \ '/g,/\ "button \"/g],
Id: [/\ 'id \ '/g,/\ "id \"/g],
Div: [/\ 'div \ '/g,/\ "div \"/g],
Body: [/\ 'body \ '/g,/\ "body \"/g],
Html: [/\ 'html \ '/g,/\ "html \"/g],
HTML: [/\ 'html \ '/g,/\ "HTML \"/g],
ParentNode: [/\ 'parentnode \ '/g,/\ "parentNode \"/g],
NextSibling: [/\ 'nextsibling \ '/g,/\ "nextSibling \"/g],
Iframe: [/\ 'iframe \ '/g,/\ "iframe \"/g],
Before: [/\ 'before \ '/g,/\ "before \"/g],
After: [/\ 'after \ '/g,/\ "after \"/g],
Script: [/\ 'script \ '/g,/\ "script \"/g],
Width: [/\ 'width \ '/g,/\ "width \"/g],
Height: [/\ 'height \ '/g,/\ "height \"/g],
Top: [/\ 'top \ '/g,/\ "top \"/g],
Left: [/\ 'left \ '/g,/\ "left \"/g],
Absolute: [/\ 'absolute \ '/g,/\ "absolute \"/g],
Relative: [/\ 'relative \ '/g,/\ "relative \"/g],
'Static ': [/\ 'static \'/g,/\ "static \"/g],
Fixed: [/\ 'fixed \ '/g,/\ "fixed \"/g],
Href: [/\ 'href \ '/g,/\ "href \"/g],
Border: [/\ 'border \ '/g,/\ "border \"/g],
Margin: [/\ 'margin \ '/g,/\ "margin \"/g],
MarginTop: [/\ 'margintop \ '/g,/\ "marginTop \"/g],
MarginBottom: [/\ 'marginbottom \ '/g,/\ "marginBottom \"/g],
MarginLeft: [/\ 'marginleft \ '/g,/\ "marginLeft \"/g],
MarginRight: [/\ 'marginright \ '/g,/\ "marginRight \"/g],
Padding: [/\ 'padding \ '/g,/\ "padding \"/g],
PaddingTop: [/\ 'paddingtop \ '/g,/\ "paddingTop \"/g],
PaddingLeft: [/\ 'paddingleft \ '/g,/\ "paddingLeft \"/g],
PaddingRight: [/\ 'paddingright \ '/g,/\ "paddingRight \"/g],
Display: [/\ 'display \ '/g,/\ "display \"/g],
Olddisplay: [/\ 'olddisplay \ '/g,/\ "olddisplay \"/g],
None: [/\ 'None \ '/g,/\ "none \"/g],
Hidden: [/\ 'den den \ '/g,/\ "hidden \"/g],
Inline: [/\ 'inline \ '/g,/\ "inline \"/g],
Opacity: [/\ 'opacity \ '/g,/\ "opacity \"/g],
Show: [/\ 'Show \ '/g,/\ "show \"/g],
Hide: [/\ 'hide \ '/g,/\ "hide \"/g],
Toggle: [/\ 'toggle \ '/g,/\ "toggle \"/g],
Json: [/\ 'json \ '/g,/\ "json \"/g],
Success: [/\ 'success \ '/g,/\ "success \"/g],
Fxshow: [/\ 'fxshow \ '/g,/\ "fxshow \"/g],
Fx: [/\ 'fx \ '/g,/\ "fx \"/g],
'. Run': [/\'. run \ '/g,/\ ". run \"/g],
'Http: ': [/\ 'HTTP: \'/g,/\ "http: \"/g],
Error: [/\ 'error \ '/g,/\ "error \"/g],
Abort: [/\ 'abort \ '/g,/\ "abort \"/g],
GET: [/\ 'get \ '/g,/\ "GET \"/g],
POST: [/\ 'Post \ '/g,/\ "POST \"/g],
Get: [/\ 'get \ '/g,/\ "get \"/g],
Filter: [/\ 'filter \ '/g,/\ "filter \"/g],
Px: [/\ 'px \ '/g,/\ "px \"/g]
};
Function callback1 (result ){
Var obj = result [0];
Var myshowEL = $ ('# myshow1 ');
MyshowEL = myshowEL. empty ();
MyshowEL. append ('<div>' + obj. type + ': <span style = "color: blue;">' + obj. num + '</span> </div> ');
}
Function callback2 (result ){
Var myshowEL = $ ('# myshow2 ');
MyshowEL = myshowEL. empty ();
For (var I = 0; I <result. length; I ++ ){
Var obj = result [I];
MyshowEL. append ('<div> "' + obj. type + '": <span style =" color: blue; ">' + obj. num + '</span> </div> ');
}
}
$ ('# Mybtn1'). click (function (){
Var $ txtarea = $ ('# myjquery ');
Var source = $ txtarea. val ();
Var val = $ ('# myinput'). val ();
If (val = ''| val. length <2 ){
Alert ('at least two characters ');
Return;
}
Var keywords = [val];
Main (keywords, source, callback1, 1)
});
$ ('# Mybtn2'). click (function (){
Var $ txtarea = $ ('# myjquery ');
Var source = $ txtarea. val ();
Main (rObj, source, callback2, 1 );
});

},1000 );
//]> </Script>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.