jquery Learning Essays (tool functions)

Source: Internet
Author: User

In jquery, the tool function of the string is only one, which is the tool function to remove the left and right spaces of the string: $.trim ().

// $.trim () remove whitespace on both sides of the string var '  '; alert (str); alert ($.trim (str ));

JQuery provides a number of tool functions for handling arrays and objects that facilitate traversal, filtering, searching, and so on for arrays or objects.

//$.each () traversing an arrayvararr = ['Zhang San','John Doe','Harry','Caifan'];$.each (arr, function (index, value) {$ ('#box'). HTML ($ ('#box'). HTML () + index +'.'+ Value +'<br/>');});//$.each () traversing an object$.each ($.ajax (), function (name, FN) {$ ('#box'). HTML ($ ('#box'). HTML () + name +'.'+'<br/><br/>');})       Note: The index in $.each () represents the number of the array element, starting from 0 by default. $.ajax () returns the Jqxhr object with methods and properties //$.grep () data filteringvararr = [5,2,9,4, One, $, the,1, at,8];varArrgrep =$.grep (arr, function (element, index) {returnElement <6&& Index <5;});    alert (ARRGREP); note: The index of the $.grep () method is calculated starting at 0. I represents the element index value//$.map () modifying Datavararr = [5,2,9,4, One, $, the,1, at,8]; Using this method, if return index, returns a Boolean value that needs to be used to determine the data needed to return thevarArrmap =$.map (arr, function (element, index) {if(Element <6&& Index <5) {returnElement +1;}}); alert (arrmap);//$.inarray () gets the subscript that finds the elementvararr = [5,2,9,4, One, $, the,1, at,8];varArrinarray = $.inarray (1, arr); alert (Arrinarray); note: the Subscript of $.inarray () is calculated starting at 0. //$.merge () merging of two arraysvararr = [5,2,9,4, One, $, the,1, at,8];varARR2 = [ at,2, the,3,6,7];alert ($.merge (arr, arr2));//$.unique () Remove duplicate DOM elements<div></div><div></div><divclass="Box"></div><divclass="Box"></div><divclass="Box"></div><div></div>varDIVs = $ ('Div').Get();d IVs= Divs.concat ($ ('. Box').Get()); Alert ($ (divs). Size ()); $.unique (divs); alert ($ (divs). Size ());//. ToArray () merging multiple DOM elements into an arrayAlert ($ ('Li'). ToArray ());

In jquery, data has various types and states. Sometimes, we want to be able to determine the type and state of the data to do the corresponding operation. JQuery provides five sets of tool functions for testing.

//determines whether an array objectvararr = [A.];alert ($.isarray (arr));//determine if it is a functionvarfn =function() {};alert ($.isfunction (FN));//determines whether an empty objectvarobj ={}alert ($.isemptyobject (obj));//determines whether objects created by {} or New object () are purely objects: objects created by {} or New object () varobj =Window;alert ($.isplainobject (obj)); Note: If you use the new Object ( ' name '); After passing a parameter, the return type is not an object, but a string, so it is not a purely primitive object. //determine if the first DOM node contains a second DOM nodeAlert ($.contains ($ (' #box '). Get (0), $ (' #pox '). Get (0)));//$.type () detection data typeAlert ($.type (window));//$.isnumeric Whether the test data is numericAlert ($.isnumeric (5.25));//$.iswindow Detecting whether a data object is a Window objectAlert ($.iswindow (window));

The URL address operation, in the previous Ajax chapter has actually been mentioned. There is only one method: $.param (), which converts the object's key-value pair into a URL-key-value pair string form.  ///$.param () Converts an object key-value pair to a URL-string key-value pair var obj =' Lee '};alert ($. param (obj));

Browser detection (deprecated)

// get Firefox and version number alert ($.browser.mozilla + ': ' + $.browser.version); Note: Firefox uses the Mozilla engine, which is generally referred to as Firefox, while Google Chrome The engine used is WebKit, which generally verifies that Chrome is using WebKit. 

$.supprot ()

The detection of browser content. For example: the transparency of the Internet is opacity, and IE's transparency is alpha. This object is $.support.

 // $.support.ajax to determine if Ajax can be created  Span style= "color: #000000;" >alert ($.support.ajax);  // $.support.opacity sets the transparency of different browsers  if  ($.support.opacity = = true   ' #box '). CSS (' opacity ', ' 0.5 '  else   {$ ( ' #box '). CSS (' filter ', ' alpha (opacity=50) ' 
  jquery provides a tool function that prepares the context of a bound function: $.proxy (). This method, 
Resolves a point-in-time issue such as this when an external event triggers a call to an object method.
// $.proxy () adjust this point Span style= "color: #0000ff;" >var obj = {name: ' Lee ' ,test: Span style= "color: #0000ff;" >function this .name);}}; $ ( ' #box '). Click (obj.test); // $ (' #box '). Click (obj, ' test '); //
 $ (function   () { var  obj={name:  ' Lee '  //this way can also change this to point to the problem 
$ (function() {    var obj={        name: ' Lee ',                test:function() {alert (this);  Alert (THIS.name)},}; Obj.test (); Pop-up Lee $ (' #box '). Click (obj.test);//Eject object htmldivelementundifined })        // like this when clicking on Div#box, this point is the object htmldivelement, THIS.name is the undifined .

jquery Learning Essays (tool functions)

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.