JQuery1.6 usage method 1

Source: Internet
Author: User
Tags string to json

Error: function (msg) {// declaration error
Throw msg;
},
ParseJSON: function (data) {// convert a string to json format
If (typeof data! = "String" |! Data) {// first determine whether the string exists
Return null;
}
Data = jQuery. trim (data); // remove the front and back Spaces
If (window. JSON & window. JSON. parse) {// The advanced version has the native JSON Conversion Function window. JSON.
Return window. JSON. parse (data );
}
If (rvalidchars. test (data. replace (rvalidescape ,"@")//
. Replace (rvalidtokens, "]") //
. Replace (rvalidbraces, "") {// a simple test is performed on {true: 1 },{ "" ,{}}......
// Rvalidchars =/^ [\], :{}\ s] * $ /,
// Rvalidescape = /\\(? : ["\\\/Bfnrt] | u [0-9a-fA-F] {4})/g, u [0-9a-fA-F] {4} matches both Chinese and English characters and escape characters
// Rvalidtokens =/"[^" \ n \ r] * "| true | false | null | -? \ D + (? : \. \ D *)? (? : [EE] [+ \-]? \ D + )? /G, // The matching string does not include "\", "\ n", "\ r", Boolean value, null, and number.
// Rvalidbraces = /(? : ^ |: | ,)(? : \ S * \ [) +/g, matching (starting with null or: Or,) (followed by zero or multiple spaces, followed by [) such [,: [, [, [, and so on
Return (new Function ("return" + data ))();
}
JQuery. error ("Invalid JSON:" + data );
},
ParseXML: function (data, xml, tmp) {do not understand the use of passing in xml tmp
If (window. DOMParser) {// Standard
// The DOMParser object parses the XML text and returns an XML Document object. To use DOMParser, use a constructor without parameters to instantiate it, and then call its // parseFromString () method:
Tmp = new DOMParser ();
Xml = tmp. parseFromString (data, "text/xml ");
} Else {// IE
Xml = new ActiveXObject ("Microsoft. XMLDOM ");
Xml. async = "false ";
Xml. loadXML (data );
}
Tmp = xml.doc umentElement;
If (! Tmp |! Tmp. nodeName | tmp. nodeName = "parsererror") {// determines whether the returned result is null, not a node, and whether an error is returned.
JQuery. error ("Invalid XML:" + data );
}
Return xml;
}
GlobalEval: function (data) {// convert data to Executable
If (data & rnotwhite. test (data) {// check whether it exists and whether it is not empty
(Window.exe cScript | function (data ){
Window ["eval"]. call (window, data); // javascscript. Other vagrants use window. eval. call (window, data );
}) (Data );
}
},
NodeName: function (elem, name ){
Return elem. nodeName & elem. nodeName. toUpperCase () = name. toUpperCase (); // determines whether the node is a node. By default, the node name is in uppercase. To ensure correct judgment, it is converted to the same format (in uppercase)
},
Each: function (object, callback, args ){
Var name, I = 0,
Length = object. length, // window, String, Function, Array type is not undefined ;..
IsObj = length = undefined | jQuery. isFunction (object); // mainly used to partition objects and Arrays
If (args) {// if no parameter is input, the default name and value are used as the parameter. if there is a parameter, the passed parameter is used.
If (isObj ){
For (name in object ){
If (callback. apply (object [name], args) === false ){
Break;
}
}
} Else {
For (; I <length ;){
If (callback. apply (object [I ++], args) === false ){
Break;
}
}
}
} Else {// if no parameter is input,
If (isObj ){
For (name in object) {// object [name] value as the current object, the name and value as the parameter
If (callback. call (object [name], name, object [name]) === false) {// judge the return value of the callback function and determine whether to continue the loop.
Break;
}
}
} Else {
For (; I <length;) {// object [I] value as the current object, the name (I is sequential) and value as the parameter
If (callback. call (object [I], I, object [I ++]) === false) {// judge the callback function return value and determine whether to continue the loop.
Break;
}
}
}
}
Return object;
},
Trim: trim? // Check whether there is a native trim method. If the parameter is text! = Blank before and after truncation. Otherwise, null is returned. If no native trim is returned, it is forcibly converted to a string and then replaced with a regular expression. Otherwise, an error is returned, use regular expression replacement (trimLeft =/^ \ s +/, // match left blank, trimRight =/\ s + $/, // match right blank ),
Function (text ){
Return text = null?
"":
Trim. call (text );
}:
Function (text ){
Return text = null?
"":
Text. toString (). replace (trimLeft, ""). replace (trimRight ,"");
},

Related Article

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.