first, take the parameter function Getparameterbyname (name) {var match = RegExp (' [? &] ' + name + ' = ([^&]*) ') in the URL . exec (Window.location.search); Return match && decodeuricomponent (Match[1].replace (/\+/g, '));} Second, regular grouping var teststr= "<Div><imgsrc= '/a.jpg 'alt= ' '/><span>Test</span><imgsrc= '/b.jpg 'alt= ' '/><span>TTest</span><imgsrc= '/c.png 'alt= ' '/></Div>"; var reg=/<img\ssrc= ' (. *?) ' \s+alt= ' \s*\/>/g;var match=reg.exec (TESTSTR), results=[];while (match! = null) {Results.push (match[1]); Match=reg.exec (TESTSTR);} Console.log (results);/*array ["/a.jpg", "/b.jpg", "/c.png"]*/Three, why parseint (1/0,19) results for 18 1/0 result is infinity, So parseint (1/0,19) is equivalent to parseint ("Infinity", 19), while in 19 binary: 19 binary 10 binary--------------------0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 A, B, C, D, E, F The result of the parseint (1/0,19) is 18, which is 18 in H. Iv. Get Set checkbox selected in jquery because after jQuery1.6. attr ("Checked") is checked, so the following two methods are generally used to get the selected state: $ ("#checkboxID"). Is (": Checked "),//jquery 1.6 +$ (" #checkboxID "). Prop (" checked "); Check the checkbox://jquery 1.6+$ ("#checkboxID"). Prop ("checked", true); $ ("#checkboxID"). Prop ("checked", false);//jquery 1.5 and below$ (' #checkboxID'). attr (' checked ', ' checked ') $ (' #checkboxID '). Removeattr (' checked ') Five, jquery determines whether an element exists if ($ (selector). Length) Vi. Encode URLs with javascript var myurl = "Http://example.com/index.html?param=1&anotherparam =2 "; var myotherurl =" Http://example.com/index.html?url= "+ encodeuricomponent (Myurl); Vii. the difference between Event.preventdefault () and return false in jquery//demo1 Event.preventdefault () $ (' a '). Click (function (e) {//Custom Handling here E.preventdefault ();}); /demo2 return false$ (' a '). Click (function () {//custom handling here return false;}; The return false in jquery is equivalent to calling both E.preventdefault and e.stoppropagation. Note that in native JS, return false is just the equivalent of calling E.preventdefault. Eight, JavaScript check whether a string is empty the simplest way if (strvalue) {//do something} IX, add and remove class//add with JavaScript classdocument.getelementby Id ("MyElement"). ClassName + = "MyClass";//remove Classdocument.getelementbyid ("MyElement"). ClassName = document.getElementById ("MyElement"). Classname.replace (/(?: ^| \s) MyClass (?! \s)/, '); X. Cancel an AJAX request in jquery var xhr = $.ajax ({type: "POST", url: "test.php", Data: "Name=test", Success:function (ms g) {alert (msg); }});//Cancel Request Xhr.abort () Note that the AJAX request can be canceled with Xhr.abort () before it responds, but if the request has reached the server side, the result is onlyAllow the browser to no longer listen for the response of this request, but the server side will still handle it. Xi. JavaScript deletes the items in the array delete vs splice var myarray=["A", "B", "C"];d elete Myarray[0];for (var i=0,j=myarray.length;i<J; i++) {Console.log (myarray[i]); /* undefined b c */}var myArray2=["A", "B", "C"];Myarray2.splice (0,1); for (var i=0,j=myarray2.length;i<j;i++) { Console.log (Myarray2[i]); /* b c */} The above code has explained the difference, one is set to undefined, one is really deleted. 12, JavaScript in 16 binary and 10 binary conversion to each other Var shex= (255). ToString (+);//ffvar iNum=parseint ("FF", +);//25513, JavaScript multi-line string how to easily write a multi-line string in JavaScript, there are three options, you choose it://onevar testhtml= "a"+ "B" + "C";//twovar testHtml2= "ABC";//threevar TESTHTML3=["A","B", "C"].join (""); 14, JavaScript!! operator is what Console.log (!! //trueconsole.log (!!) 0);//falseconsole.log (!! " ABC ");//trueconsole.log (!!"); /false is simply to convert the value on the right to bool value 15, JavaScript implementation EndsWith String.prototype.endsWith= function (suffix){return this.indexof (suffix, this.length-suffix.length)!== -1;};/ /orfunction endsWith (str, suffix) {return str.indexof (suffix, str.length-suffix.length)!== -1;} 16, JavaScript Cloning object function Clone (obj) {//Handle the 3 simple types, and null or undefined if (null== obj | | "Object"!= typeofobj) return obj; Handle date if (obj instanceof date) {var copy= newDate (); Copy.settime (Obj.gettime ()); return copy; }//Handle array if (obj instanceof array) {var copy= []; For (var i= 0,var len= obj.length;i < Len; ++i) {Copy[i]= Clone (Obj[i]); } return copy; }//Handle object if (obj instanceof object) {var copy= {}; For (var attr in obj) {if (Obj.hasownproperty (attr)) copy[attr]= Clone (Obj[attr]); } return copy; } throw new Error ("Unable to copy obj! Its type isn ' t supported. "); 17, the conversion between JavaScript characters and ASCII code console.log ("\ n". charCodeAt (0));//10console.log (String.fromCharCode);//a 18, The equal judgment of floating-point numbers in JavaScript is not available== Console.log (0.1+0.2== 0.3);//falseconsole.log (Math.Abs (0.1+0.2-0.3) < 0.000001);//true as shown above, the floating-point number is equal to be judged by the absolute value of the difference less than a certain number. As for the reasons can be referred to here: Http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html 19, JavaScript base64 code var Base64= {//Private PROPERTY_KEYSTR: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/= ",//Public method for Encodingencode:function (input) {var output =""; var chr1, CHR2, CHR3, Enc1, Enc2, enc3, Enc4; var i= 0; input= Base64._utf8_encode (input); While (I < input.length) {CHR1= Input.charcodeat (i++); CHR2= Input.charcodeat (i++); CHR3= Input.charcodeat (i++); enc1= Chr1>> 2; ENC2 = ((Chr1 & 3)<< 4) | (CHR2>> 4); Enc3 = ((CHR2 &)<< 2) | (Chr3>> 6); Enc4 = CHR3 & 63; if (IsNaN (CHR2)) {enc3 = Enc4 = 64; } else if (IsNaN (CHR3)) {Enc4 = 64; } output = output + This._keystr.charat (ENC1) + This._keystr.charat (ENC2) + This._keystr.charat (enc3) + This._keystr.charat (ENC4); Return output;},//public method for Decodingdecode:function (input) {var output = ""; var chr1, CHR2, CHR3; var enc1, Enc2, enc3, Enc4; var i = 0; input = Input.replace (/[^a-za-z0-9\+\/\=]/g, ""); while (I<input. Length) {ENC1= This._keystr.indexof (Input.charat (i++)); ENC2= This._keystr.indexof (Input.charat (i++)); enc3= This._keystr.indexof (Input.charat (i++)); Enc4= This._keystr.indexof (Input.charat (i++)); CHR1= (enc1<< 2) | (Enc2>> 4); CHR2 = ((Enc2 &)<< 4) | (Enc3>> 2); CHR3 = ((enc3 & 3)<< 6) | Enc4; Output= Output+ string.fromcharcode (CHR1); if (enc3! )= +){Output= Output+ string.fromcharcode (CHR2); } if (Enc4! )= +){Output= Output+ string.fromcharcode (CHR3); }} Output= Base64._utf8_decode (output); return output;},//Private method for UTF-8 encoding_utf8_encode:function (string) {string= String.Replace (/\r\n/g, "\ n"); var utftext= ""; for (var n= 0;N < string.length; n++) {var c= String.charcodeat (n); if (c < d) {Utftext += String.fromCharCode (c); } else if ((C>127) && (c<2048) ) {Utftext += String.fromCharCode ((c>> 6) | 192); Utftext + = String.fromCharCode ((C & 63) | 128); } else {utftext + = String.fromCharCode ((c >> 12) | 224); Utftext + = String.fromCharCode (((c >> 6) & 63) | 128); Utftext + = String.fromCharCode ((C & 63) | 128); }} return utftext;},//private method for UTF-8 decoding_utf8_decode:function (utftext) {var string = ""; var i = 0; var c = C1 = C2 = 0; while (I<Utftext. Length) {C= Utftext.charcodeat (i); if (c < d) {string += String.fromCharCode (c); i++; } else if ((C>191) && (c<224) ) {C2= Utftext.charcodeat (i+1); string += String.fromCharCode ( ((c&) << 6) | (C2 & 63)); i += 2; } else {c2= Utftext.charcodeat (i+1); C3= Utftext.charcodeat (i+2); string += String.fromCharCode ( ((c&) << 12) | ((C2 &) << 6) | (C3 & 63)); i += 3; }} return string;}} Encodebase64.encode ("Test"); Vgvzda==//decodebase64.decode ("Vgvzda=="); //Test 20, the difference between each and map in jquery each and map can be used to traverse an array or object, except that each does not change the original array or object, Map is the operation of the given array or object to return a new array or object. Demo:var Items= [1,2,3,4];$.each (items, function () {alert (' This was ' + this);//alert 1,2,3,4}); var newitems= $.map (items,function (i) {return i + 1;}); /NewItems is [2,3,4,5] map will take up more memory, so if you just traverse suggestions with each. 21. Determine if an object is an array function IsArray (obj) {return Object.prototype.toString.call (obj)== "[Object Array]";} Can not use instanceof and constructor to judge, reason reference: http://perfectionkills.com/ instanceof-considered-harmful-or-how-to-write-a-robust-isarray/22. Create a new object with prototype inheritance function inherit (p) {if (!p) {T Hrow TypeError ("P is not a object or null"); } if (object.create) {return object.create (p); } var t=typeofp; if (t!== "Object" && t!== "function") {Throw TypeError ("P is not a object or null"); } function f () {}; F.prototype=p; return new F ();
Original from: http://www.cnblogs.com/jscode/archive/2012/07/25/2605395.html
These years, I collected the JavaScript code (a)