Jquery entry (8): Tools

Source: Internet
Author: User

Tools

Note:JqueryCan be replaced with $

Browser
Jquery. browser ()
: The ID of the browser kernel. Depends on navigator. useragent.

Available value: safari opera MSIE Mozilla

Returns true in Microsoft's Internet Explorer.

Code: $. Browser. MSIE

Only prompts "this is safari!" in safari! ".

Code:

If ($. browser. Safari ){

Alert ("this is safari! ");

}

 

Jquery. browser. Version (): Browser Rendering Engine Version

Typical results:

Internet Explorer: 6.0, 7.0

Mozilla/Firefox/flock/Camino: 1.7.12, 1.8.1.3

Opera: 1, 9.20

Safari/WebKit: 312.8, 418.9

 

If ($. browser. MSIE)

Alert ($. browser. version );

 

Jquery. boxmodel (): Whether the browser uses the standard box model to render the page on the current page

Returns false in quirksmode.

Code: $. boxmodel

 

Array and object operations
Jquery. Each (OBJ, callback)
: Common example method, which can be used for example object and array.

Code: for example, repeat the array and use the element index and content at the same time.

$. Each ([0, 1, 2], function (I, n ){

Alert ("item #" + I + ":" + n );

});

 

Code: for example, the object is repeated, and the member name and variable content are used at the same time.

$. Each ({name: "John", Lang: "JS"}, function (I, n ){

Alert ("name:" + I + ", value:" + n );

});

 

jquery. extend (target, obj1, objn) : use one or more other objects to expand an object , returns the extended object

Code:MergeSettingsAndOptions,Modify and returnSettings.

VaR settings = {validate: false, limit: 5, name: "foo "};

VaR Options = {validate: True, name: "bar "};

Jquery. Extend (settings, options );

Result: Settings = {validate: True, limit: 5, name: "bar "}

 

Code:MergeUltsAndOptions,Do not modifyUlts.

VaR empty = {}

VaR defaults = {validate: false, limit: 5, name: "foo "};

VaR Options = {validate: True, name: "bar "};

VaR settings = jquery. Extend (empty, defaults, options );

Result:

Settings = {validate: True, limit: 5, name: "bar "}

Empty = {validate: True, limit: 5, name: "bar "}

 

Jquery. grep (array, callback, invert):Filter Array elements using filter functions

Code: filter elements smaller than 0 in the array.

$. Grep ([0, 1, 2], function (n, I ){

Return n> 0;

});

Result: [1, 2]

 

Code: exclude elements larger than 0 in the array. Use the third parameter to exclude them.

$. Grep ([0, 1, 2], function (n, I ){

Return n> 0;

}, True );

Result: [0]

 

Jquery. makearray (OBJ): Convert a class array object to an array object

<Div> first </div> <div> second </div> <div> third </div> <div> fourth </div>

Code: filter elements smaller than 0 in the array.

VaR arr = jquery. makearray (document. getelementsbytagname ("Div "));

Arr. Reverse ();//Use the array flip Function

Result:

Fourth

Third

Second

First

 

Jquery. Map (array, callback):Convert the elements in one array to another.

Code: Add 4 to each element in the original array to convert it into a new array.

$. Map ([0, 1, 2], function (n ){

Return N + 4;

});

Result: [4, 5, 6]

 

Code: Add 1 to element 0 in the original array; otherwise, delete the array.

$. Map ([0, 1, 2], function (n ){

Return n> 0? N + 1: NULL;

});

Result: [2, 3]

 

Code: each element in the original array is extended into an array containing itself and its value plus 1, and converted into a new array.

$. Map ([0, 1, 2], function (n ){

Return [N, N + 1];

});

Result: [0, 1, 1, 2, 2, 3]

 

Jquery. Unique (array):Delete repeated elements in the array

Code:Duplicate DeletionDivLabel.

$. Unique (document. getelementsbytagname ("Div "));

Result: <div>, <div> ,...

 

Test Operation
Jquery. isfunction (OBJ)
: Whether the test object is a function

Code: checks whether a function is used.

Function stub (){}

VaR objs = [

Function (){},

{X: 15, Y: 20 },

Null,

Stub,

"Function"

];

Jquery. Each (objs, function (I ){

VaR isfunc = jquery. isfunction (objs [I]);

$ ("Span: eq (" + I + ")"). Text (isfunc );

});

Result: True, false, false, true, false

 

String operation
Jquery. Trim (STR)
:Removes spaces at the start and end of a string.

$. Trim ("Hello, how are you?"); Result:" Hello, how are you? "

 

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.