List of common jquery techniques and methods _ jquery-js tutorial

Source: Internet
Author: User
List of common jquery skills and methods. Anyone who learns jquery can add them to your favorites. Easy to find and learn. 1. Reference of page elements
The $ () referenced element of jquery includes methods such as id, class, element name, element hierarchy, dom or xpath conditions, and the returned object is a jquery object (set object ), you cannot directly call methods defined by the dom.
2. Conversion between jQuery objects and dom objects
Only jquery objects can use methods defined by jquery. Pay attention to the differences between dom objects and jquery objects. When calling methods, pay attention to dom objects or jquery objects.
Common dom objects can be converted to jquery objects through $.
For example, $ (document. getElementById ("msg") is a jquery object. You can use the jquery method.
Because the jquery object itself is a set. Therefore, if a jquery object is to be converted to a dom object, You must retrieve one of them. Generally, you can retrieve it through indexes.
For example: $ ("# msg") [0], $ ("p "). eq (1) [0], $ ("p "). get () [1], $ ("td") [5] are all dom objects. You can use methods in dom, but you cannot use Jquery methods.
The following statements are correct:
3. How to obtain a certain entry of the jQuery set
For the obtained element set, you can use the eq or get (n) method or index number to obtain one of the items (specified by the index). Note that the returned eq is a jquery object, get (n) and index return dom element objects. Jquery objects can only use jquery methods, while dom objects can only use dom methods. For example, to obtain the third
Element Content. There are two methods:
$ ("P" ).eq(2).html (); // call the jquery object Method
$ ("P"). get (2). innerHTML; // call the dom method attribute
4. Implement set and get for the same function
Many methods in Jquery are like this, mainly including the following:
$ ("# Msg" ).html (); // return the html content of the element node whose id is msg.
// Write "new content" as an html string to the element node content with the id of msg. the bold new content is displayed on the page.
$ ("# Msg"). text (); // return the text content of the element node whose id is msg.
// Write "new content" as a common text string to the element node content with the id of msg. The page displays the new content
$ ("# Msg"). height (); // returns the height of the element whose id is msg.
$ ("# Msg"). height ("300"); // set the height of the element whose id is msg to 300
$ ("# Msg"). width (); // return the width of the element whose id is msg
$ ("# Msg"). width ("300"); // set the width of the element whose id is msg to 300
$ ("Input"). val ("); // return the value of the input box of the form.
$ ("Input"). val ("test"); // set the value of the form input box to test
$ ("# Msg"). click (); // trigger the click event of an element whose id is msg
$ ("# Msg"). click (fn); // click an event to add a function for an element whose id is msg
Similarly, blur, focus, select, and submit events can all have two call methods.
5. Set Processing
For the set content returned by jquery, we do not need to iterate through it and process each object separately. jquery has provided us with a very convenient way to process the set.
There are two forms:
// Set different font colors for p elements whose indexes are 0, 1 and 2 respectively.
// Implement the Color Changing Effect of the row separation of the table
// Adds a click event for each p element. click a p element to bring up its content.
6. Expand the functions we need
}); // Added the min and max methods for jquery.
Use an extended method (called by "$. Method Name ):
7. method-based writing
The so-called continuous writing means that different methods can be called continuously for a jquery object.
For example:
8. Style of operation elements
It mainly includes the following methods:
$ ("# Msg" ).css ("background"); // return the background color of the element.
$ ("# Msg" ).css ("background", "# ccc") // set the element background to Gray.
$ ("# Msg"). height (300); $ ("# msg"). width ("200"); // set the width and height.
$ ("# Msg" ).css ({color: "red", background: "blue"}); // set the style as a name-Value Pair
$ ("# Msg"). addClass ("select"); // Add a class named select for the element
$ ("# Msg"). removeClass ("select"); // Delete the class whose element name is select
$ ("# Msg"). toggleClass ("select"); // Delete (ADD) the select class if it exists (does not exist)
9. Complete event handling functions
Jquery already provides various event handling methods. Instead of Directly Writing events on html elements, we can directly add events to objects obtained through jquery.
For example:
$ ("# Msg"). click (function () {}) // added a click event for the element
// Set different processing for three p-element click events
Several custom events in jQuery:
(1) hover (fn1, fn2): a method that imitates a hover event (move the mouse over an object and remove it. When you move the cursor over a matching element, the specified first function is triggered. When you move the cursor out of this element, the specified second function is triggered.
// When you place the cursor over a row of the table, set the class to over and the class to out when you leave the table.
(2) ready (fn): binds a function to be executed when the DOM is loaded and can be queried and manipulated.
// After the page is loaded, the message "Load Success" is displayed, which is equivalent to an onload event. Equivalent to $ (fn)
(3) toggle (evenFn, oddFn): switches the function to be called each time you click. If a matching element is clicked, the specified first function is triggered. When the same element is clicked again, the specified second function is triggered. The subsequent clicks repeatedly call the two functions.
// Add or delete a class named selected every time you click it.
(4) trigger (eventtype): triggers an event on each matching element.
For example:
$ ("P"). trigger ("click"); // triggers the click event of all p elements
(5) bind (eventtype, fn) and unbind (eventtype): bind and unbind events
Deletes a bound event from each matching element.
For example:
$ ("P"). bind ("click", function () {. text () ;}); // Add a click event for each p element
$ ("P"). unbind (); // delete all events on all p elements
$ ("P"). unbind ("click") // Delete the click Event on all p elements
10. Some practical special effects
The toggle () and slidetoggle () methods provide the status switching function.
For example, the toggle () method includes the hide () and show () methods.
The slideToggle () method includes the slideDown () and slideUp methods.
11. Several useful jQuery Methods
$. Browser. browser type: Check the browser type. Valid parameters: safari, opera, msie, and mozilla. If ie: $. browser. isie is detected, true is returned for ie.
$. Each (obj, fn): A common iteration function. It can be used to iterate objects and arrays (instead of loops) in an approximate way ).
For example
It is equivalent:
Json data can also be processed, such
$. Extend (target, prop1, propN): expands an object with one or more other objects and returns the extended object. This is an inheritance method implemented by jquery.
For example:
// Merge settings and options, and return the Merged Results to settings, which is equivalent to inheriting setting and saving the inherited results in setting.
// Merge ults and options, and return the merged result to setting without overwriting the default content.
There can be multiple parameters (merging multiple parameters and returning them)
$. Map (array, fn): array ing. Save the items in an array (after conversion) to another new array and return the new array.
For example:
TempArr content: [4, 5, 6]
TempArr content: [2, 3]
$. Merge (arr1, arr2): merges two arrays and deletes repeated items.
For example: $. merge ([, 2], [, 4]) // return [, 4]
$. Trim (str): removes spaces at both ends of a string.
For example: $. trim ("hello, how are you? "); // Return" hello, how are you? "
12. Resolve conflicts between custom methods or other class libraries and jQuery
Most of the time, we define the $ (id) method to obtain an element, or some other js class libraries such as prototype define the $ method, if these contents are put together at the same time, the variable method definition conflict will occur. Jquery provides a dedicated method to solve this problem.
Use the jquery. noConflict (); Method in jQuery to assign control of the variable $ to the first library or the previously customized $ method to implement it. When Jquery is applied, you only need to replace all $ with jQuery. For example, the original reference object method $ ("# msg") is changed to jQuery ("# msg ").
For example:
// Start using jQuery
// Use $ () for other libraries ()
List of common jquery methods:
$ (P). addClass (style type defined in css); add a style to an element
$ (Img). attr ({src: test.jpg, alt: test Image}); add an attribute/value to an element. The parameter is map.
((Img).attr(src,test.jpg); add attributes/values to an element
$ (Img). attr (title, function () {return this. src}); add attributes/values to an element
((Element .html (); obtain the content (elements, text, etc.) in the element)
Metadata (metadata .html (new stuff); set content for an element
$ (Element name). removeAttr (attribute name) deletes a specified attribute and its value for an element.
$ (Element name). removeClass (class) deletes a specified style for an element
$ (Element name). text (); get the text of the element
$ (Element name). text (value); set the text value of this element to value
$ (Element name). toggleClass (class) cancels when the element has a style in the parameter. If it does not exist, set this style.
$ (Input element name). val (); get the value of the input element
$ (Input element name). val (value); set the value of the input element to value
$ (Element name). after (content); add content after Matching Element
$ (Element name). append (content); insert content as the element content to the end of the element
$ (Element name). appendTo (content); element after content
$ (Element name). before (content); opposite to the after Method
$ (Element name). clone (Boolean expression) when the Boolean expression is true, the cloned element (treated as true if there is no parameter)
$ (Element name). empty () set the content of this element to null
$ (Element name). insertAfter (content); insert the element to the content
$ (Element name). insertBefore (content); insert the element before the content
$ (Element). prepend (content); Use content as a part of the element and put it at the beginning of the element
$ (Element). prependTo (content); Use this element as a part of content and put it at the beginning of content
$ (Element). remove (); Delete all specified elements
$ (Element). remove (exp); Delete all elements containing exp
$ (Element). wrap (html); Use html to enclose this element
$ (Element). wrap (element); Use element to enclose this element
$ (Html). appendTo (body) is equivalent to writing an html code in the body.
$ (Elems) gets an element in the DOM
$ (Function () {...}); execute a function
$ (P> p;.css (border, 1px solid gray); find the subnode p of all p and add the style
$ (Input: radio, document. forms [0]) Search for all radio buttons in the first form on the current page
$. Extend (prop) prop is a jquery object,
JQuery (expression, [context]) $ (expression, [context]); by default, $ () queries the DOM elements in the current HTML document.
Each (callback) executes a function using each matching element as the context.
Example: 1
Example: 2
Ready (fn); $ (document). ready () Note that there is no onload event in the body; otherwise, this function cannot be executed. You can
Many functions are loaded and executed in the fn order.
Bind (type, [data], fn) binds one or more event processor functions for a specific event that matches an element, such as click. Possible event attributes include: blur, focus, load, resize, scroll, unload, click, dblclick, mousedown, mouseup, mousemove,
One (type, [data], fn) binds one or more event processor functions for a specific event that matches an element, such as click. In each pair
As shown in the preceding figure, the event processing function is executed only once. Other rules are the same as those of the bind () function.
Trigger (type, [data]) triggers an event on each matching element.
TriggerHandler (type, [data]) triggers a specific event on an element (specify an event type), and cancels the default action of the browser on this event.
Unbind ([type], [data]) is unbound from each matching element.
$ (P). unbind () removes all bound events in all paragraphs
$ (P). unbind (click) removes the click Event on all paragraphs
Hover (over, out) over and out are all methods. When you move the cursor over a matching element, the specified first function is triggered. When you move the cursor out of this element, the specified second function is triggered.
Toggle (fn, fn) triggers the specified first function if a matching element is clicked. When you click the same element again, the second function is triggered.
Element event list description
Note: For a function without parameters, the parameter is optional fn. JQuery does not support reset events of form elements.
Event Description supports elements or objects.
Blur () element loses focus a, input, textarea, button, select, label, map, area
Change () user changes the field content input, textarea, select
Click () the mouse clicks almost all elements of an object.
Dblclick () double-click almost all elements of an object
Error () an error occurs when a document or image is loaded. window, img
The focus () element obtains the focus a, input, textarea, button, select, label, map, and area.
Keydown () A keyboard key is pressed by almost all elements
Keypress () A keyboard key is pressed or pressed to almost all elements
Keyup () A keyboard key is released to almost all elements
Load (fn) a page or image is loaded. window, img
Mousedown (fn) a mouse button is pressed by almost all elements
Mousemove (fn) the mouse is moved to almost all elements
Mouseout (fn) move the mouse over an element to remove almost all elements.
Mouseover (fn) move the mouse over a certain element to almost all elements
Mouseup (fn) a mouse button is released to almost all elements
Resize (fn) window or frame size adjusted window, iframe, frame
Window when scroll (fn) is rolling the visible part of the document
Select () text selected document, input, textarea
The submit () submit button is clicked by form
Unload (fn) User exit page window
JQuery Ajax method description:
Load (url, [data], [callback]) loads a remote HTML content to a DOM node.
((#Feeds).load(feeds.html); load the feeds.html file to the p with the id of feeds
JQuery. get (url, [data], [callback]) uses GET to request a page.
JQuery. getJSON (url, [data], [callback]) uses GET to request JSON data.
JQuery. getScript (url, [callback]) uses GET to request and execute javascript files.
JQuery. post (url, [data], [callback], [type]) uses POST to request a page.
AjaxComplete (callback) executes a function after an AJAX request is completed. This is an Ajax event
AjaxError (callback) executes a function when an AJAX request fails. This is an Ajax event
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: 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.