A discussion on the class array operation in the jquery object

Source: Internet
Author: User
Tags filter expression wrapper

We all know that the jquery object has a set of element wrappers that resemble an array in JS that has the length property, so we call this the class array, and here we'll summarize how the class array in this jquery object operates. Look at how our jquery provides us with the available methods:

Size (): Obviously, it should return the number of elements in the packing set, such as $ (' a '). Size () represents the number of linked elements;

Get (Index): When the index is not specified, all elements of the wrapper set are taken by default and returned as an array in JS, and if index is specified, the element corresponding to index is returned, such as $ (' img[title] '). Get (0) Returns the first IMG element containing the title of the property, which is also equivalent to $ (' img[title] ') [0];

Index (Elem): Returns the subscript of the element Elem in the wrapper set and returns 1 if the element is not found;

Add (string|elem| Array): Adds the element in the parameter to the packing set, and if the parameter is a jquery selector, all matching objects are added to the collection, and if it is an HTML element, the array of elements obtained by the Clean method is added to the collection, if it is a DOM element or an array of DOM elements, It is added directly to the collection; note that the added wrapper set is returned; $ (' img[alt] ', ' img[title] ') is equivalent to $ (' img [Alt] '). Add (' img[title] ') That is, an IMG element containing the ALT attribute or an IMG element containing the title attribute is returned;

Not (string|elem| Array): Removes the element that satisfies the parameter conditions in the packing set, noting that the parameter can only be a filter expression, that is, an expression that begins with "[" or ":", such as $ (' img[title] '). Not (' Title*=puy ') returns an IMG element that contains the title property and and the title attribute of these elements contains Puy text, and the wrapper set after the filter is returned;

Filter (string|function): If the incoming argument is a String type, then the expression must also be a filter expression that deletes all elements that do not match the selector from the wrapper set; If a function is passed in, then the wrapper This function is called for each element in the set, and if the function returns False, the element is removed from the wrapper set, and the element in the function that calls the method in the wrapper set is called by the This keyword, such as $ (' TD '). Filter (function () {return This.innerHTML.match (/^ "d+$/)}" returns all TD elements in TD with the contents of a number;

Slice (begin,end): Creates and returns a new wrapper set, which is a contiguous part of the original wrapper set, and the first element of the new wrapper set is the element of the begin position in the original packing set, and the last element is the previous element of the end position element, which, of course, can be unspecified. Then it will extend to the end of the original wrapper set; such as $ (' * '). Slice (2,3) This statement selects all the elements on the page, and then survives the new wrapper set that contains the third element of the original wrapper set, noting this $ (' * '). Get (2) is different, this returns the element, And the slice method returns the wrapper set, thus owning the operation of the wrapper set;

Childen (): Returns a new wrapper set consisting of all the different child elements of the original wrapper set element (without the text node). such as $ (' div '). Children () returns a new wrapper set consisting of child elements under all DIV elements, or a filter expression if a parameter is specified;

Contents (): Returns the contents of the original wrapper set element new wrapper set (can contain text nodes); Note that this method cannot accept parameters for filtering;

Next (): Returns a new wrapper set consisting of all the unique next sibling elements of the original wrapper set element; If you specify a parameter, the parameter is also a filter expression; next () returns the next instance of the DIV element containing the ID somediv. The wrapper set of the sibling element; If the argument is specified, then the parameter is also a filter expression;

Nextall (): Returns a new wrapper set containing all subsequent siblings of the original wrapper set element, such as $ (' Div#somediv '). Nextall () returns a new wrapper set containing the trailing sibling elements of a DIV element with an ID of somediv, if an argument is specified, then the parameter Number is also a filter expression;

Parent (): Returns the new wrapper set of the unique direct parent element for all elements of the original wrapper set, or the filter expression if the parameter is specified;

Parents (): Returns the new wrapper set for the ancestor element of all elements of the original wrapper set; If the parameter is specified, then the parameter is also a filter expression;

Prev (): Returns a new wrapper set consisting of all unique previous sibling elements of the original wrapper set element, or a filter expression if the argument is specified;

Prevall (): Returns a new wrapper set containing all the front sibling elements of the original wrapper set element, or a filter expression if the parameter is specified;

Siblings (): Returns a new wrapper set consisting of all the unique sibling elements in the original wrapper set element, or a filter expression if the parameter is specified;

Find (String): Returns a new wrapper set containing all the elements in the original wrapper set that match the incoming selector expression, and the descendants of the elements in the original wrapped set are also passed into the new wrapper set;

Contains (text): Returns a new wrapper set consisting of elements that contain the text string passed in by the text parameter;

Is (string): Returns True if the wrapper set contains String-matching elements, otherwise returns false;

Clone (Copyhandle): If the passed parameter is true, then the event will be copied together, otherwise do not copy the event, forming a new packing set;

End (): Called within the jquery command chain to return to the previous wrapper set;

Andself (): Merge the most recently generated two packing sets within the chain of command;

For packaging elements of the operation summed up here, if where wrong also please point out to facilitate the reference for everyone to learn.

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.