Home > Article > Web Front-end > Summary of some common methods that come with jQuery_jquery
Method itself ($.each,$.map,$.contains,$ajax)
Common tools and methods
(1)$.trim
The $.trim method is used to remove excess spaces at the beginning and end of the string.
(2)$.contains
The $.contains method returns a Boolean value indicating whether a DOM element (second parameter) is a subordinate element of another DOM element (first parameter).
(3) $.each, $.map
The $.each method is used to iterate through arrays and objects, and then return the original object. It accepts two parameters, which are the data collection and the callback function.
The $.map method is also used to traverse arrays and objects, but it will return a new object.
(4)$.inArray
The $.inArray method returns the position of a value in the array (starting from 0). If the value is not in the array, -1 is returned.
(5)$.extend
$.extend method is used to merge multiple objects into the first object.
(6)$.proxy
The $.proxy method is similar to the bind method of ECMAScript 5. It can bind the context of the function (that is, this object) and parameters and return a new function.
The main use of jQuery.proxy() is to bind context objects to callback functions.
Another equivalent way of writing this example is:
This example shows that there are two main ways to write the proxy method.
Look at another example. Under normal circumstances, the this object in the following code points to the DOM object where the click event occurs.
At this time, you can use the proxy method to bind this object to the myElement object.
(7)$.data, $.removeData
$.data method can be used to store data on DOM nodes.
The above code stores a key-value pair on the web page element body, with the key name "foo" and the key value 52.
The $.removeData method is used to remove the data stored in the $.data method.
(8)$.parseHTML, $.parseJSON, $.parseXML
The $.parseHTML method is used to parse strings into DOM objects.
The $.parseJSON method is used to parse a JSON string into a JavaScript object, similar to the native JSON.parse(). However, jQuery does not provide a method similar to JSON.stringify(), that is, it does not provide a method to convert JavaScript objects into JSON objects.
The $.parseXML method is used to parse a string into an XML object.
(9)$.makeArray
The $.makeArray method converts an array-like object into a real array.
Methods to determine data type
jQuery provides a series of tool methods for determining data types to make up for the shortcomings of JavaScript's native typeof operator. The following method judges the parameters and returns a Boolean value.
jQuery.isArray(): Whether it is an array.
jQuery.isEmptyObject(): Whether it is an empty object (does not contain enumerable properties).
jQuery.isFunction(): Whether it is a function.
jQuery.isNumeric(): Whether it is an array.
jQuery.isPlainObject(): Whether it is an object generated using "{}" or "new Object" instead of an object provided natively by the browser.
jQuery.isWindow(): Whether it is a window object.
jQuery.isXMLDoc(): Determines whether a DOM node is in an XML document.
Here are some examples.
Ajax operation
$.ajax
The jQuery object also defines an Ajax method ($.ajax()) to handle Ajax operations. After calling this method, the browser will send an HTTP request to the server.
$.ajax() can be used in many ways, the most common is to provide an object parameter.
async: This item defaults to true. If set to false, it means that a synchronous request is issued.
cache: This item defaults to true. If set to false, the browser will not cache the data returned by the server. Note that the browser itself will not cache the data returned by the POST request, so even if it is set to false, it will only be effective for HEAD and GET requests.
url: server-side URL. This is the only required attribute, other attributes can be omitted.
type: HTTP verb used to send information to the server. The default is GET. Other verbs include POST, PUT, and DELETE.
dataType: The data type requested from the server, which can be set to text, html, script, json, jsonp and xml.
data: Data sent to the server. If the GET method is used, this item will be converted into a query string and appended to the end of the URL.
success: callback function when the request is successful. The function parameters are the data returned by the server, status information, and the original object that issued the request.
timeout: The maximum number of milliseconds to wait. If the request has not returned after this time, the request status will be automatically changed to failed.
error: callback function when the request fails. The function parameters are the original object that issued the request and the returned status information.
complete: A callback function that will be executed regardless of whether the request succeeds or fails. The function parameters are the original object that issued the request and the returned status information.
Among these parameters, url can be independent and used as the first parameter of the ajax method. In other words, the above code can also be written as follows.
Simple writing method
There are also some simple ways to write the ajax method.
$.get(): Make a GET request.
$.getScript(): Read a JavaScript script file and execute it.
$.getJSON(): Issue a GET request to read a JSON file.
$.post(): Make a POST request.
$.fn.load(): Read an html file and put it into the current element.
Generally speaking, these convenience methods accept three parameters in order: url, data, and callback function on success.
(1)$.get(), $.post()
These two methods correspond to the GET method and POST method of HTTP respectively.
The ajax writing method corresponding to the above post method is as follows.
(2)$.getJSON()
Another simple way to write the ajax method is the getJSON method. When the server returns data in JSON format, this method can be used instead of the $.ajax method.
(3)$.getScript()
$.getScript method is used to load a script file from the server side.
The callback function of getScript accepts three parameters, which are the content of the script file, the status information of the HTTP response and the ajax object instance.
(4)$.fn.load()
$.fn.load is not a tool method of jQuery, but a method defined on the jQuery object instance, which is used to obtain the server-side HTML file and put it into the current element. Since this method also belongs to ajax operation, it will be discussed here together.
Ajax event
jQuery provides the following methods for specifying callback functions for specific AJAX events.
.ajaxComplete(): The ajax request is completed.
.ajaxError(): Ajax request error.
.ajaxSend(): before the ajax request is issued.
.ajaxStart(): The first ajax request starts to be issued, that is, there is no ajax request yet to be completed.
.ajaxStop(): After all ajax requests are completed.
.ajaxSuccess(): After the ajax request is successful.
Below are examples.
Return value
The ajax method returns a deferred object, and you can use the then method to specify a callback function for the object (see the "Deferred Object" section for detailed explanation).
JSONP
Due to the "same domain restriction" in the browser, the ajax method can only make HTTP requests to the domain name of the current web page. However, by inserting a script element (