Home > Article > Web Front-end > What is a jquery class array? How does it work?
What is a class array?
The jQUery object has an array-like element packaging set. This set has the length property like an array in js, so we call this an array-like set.
In many cases, JQuery's $() function returns a JQuery object similar to data. For example, $('div') will return the JQuery object wrapped by all div elements in the div. In this case, JQuery provides several commonly used properties and methods to manipulate JQuery objects.
length: This property returns the number of DOM elements contained in JQuery.
context: This property returns the context parameter passed in to get the JQuery object
JQuery: This property returns the JQuery version
each(fn(index)): This method is Is an iterator function that will use the fn function to iterate through each element contained in JQuery.
get(): This method returns an array consisting of all DOM elements contained in JQuery.
get(index): This method returns the index+1th DOM element contained in JQuery.
toArray(): Convert all DOM objects contained in the JQuery object into data.
How to operate the class array in jQuery object?
Look at what available methods our jQuery provides us with:
Size(): Obviously, it should return the number of elements in the package set, such as $('a ').size() represents the number of link elements;
Get(index): When index is not specified, all elements in the packaging set will be taken by default and returned as an array in js. If index is specified , then the element whose subscript is index is returned, such as $('img[title]').get(0) returns the first img element containing the attribute title, which is also equivalent to $('img[title]' )[0];
Index(elem): Returns the subscript of the element elem in the packaging set. If the element is not found, -1 is returned;
add(String|elem|Array ): Add the elements in the parameter to the packaging set. If the parameter is a jQuery selector, then all matching objects will be added to the set. If it is html element, then the element obtained through the clean method The array 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 what is returned is the added packaging set; such as $('img[alt]','img[title]') Equivalent to $('img[alt]').add('img[title]'), which returns an img element containing the alt attribute or an img element containing the title attribute;
not(String|elem|Array ): Delete the elements that meet the parameter conditions in the packaging set. Note that parameters can only be filtering expressions , that is, expressions starting with "[" or ":", such as $( 'img[title]').not('title*=puy') returns img elements that contain the title attribute, and the title attribute of these elements contains the puy text; what is returned is the filtered packaging set;
filter(String|function): If the parameter passed in is of type String, then the expression must also be a filter expression, used to delete all elements that do not match the selector from the packaging set; if a function is passed in If so, then each element in the packaging set calls this function. If this function returns false, the element will be deleted from the packaging set. In the function, the this keyword can be used to call the element in the packaging set that calls the method at that time; such as $( 'td').filter(function(){return this.innerHTML.match(/^"d+$/)}) returns all td elements whose content in td is numbers;
slice(begin,end): create And returns a new packaging set, which is a continuous part of the original packaging set, and the first element of the new packaging set is the element at the begin position of the original packaging set, and the last element is the element before the end position element , of course end does not need to be specified, then it will extend to the end of the original packaging set; such as $('*').slice(2,3), this statement selects all elements on the page, and then saves the third one containing the original packaging set The new packaging set of the element. Note that this $('*').get(2) is different. This returns the element, while the slice method returns the packaging set, thus having the operation of the packaging set;
childen(): Returns a new packaging set composed of all different child elements of the original packaging set element (excluding text nodes), such as $('div').children() returns all div elements A new packaging set composed of child elements; if a parameter is specified, the parameter is also a filter expression;
Contents(): Returns the new packaging set of the contents of the original packaging set elements (can include text nodes); note that this method cannot Accept parameters for filtering;
Next(): Returns a new packaging set composed of all unique next sibling elements of the original packaging set element; if a parameter is specified, then the parameter is also a filtering expression; such as $('div #someDiv').next() returns the wrapping set containing the next sibling element of the div element with id someDiv; if a parameter is specified, the parameter is also a filter expression;
nextAll(): returns the wrapping set containing the original A new wrapping set of all subsequent siblings of the element; for example, $('div#someDiv').nextAll() returns a new wrapping set containing the subsequent sibling elements of the div element with id someDiv; if a parameter is specified, then this parameter is also a filter Expression;
parent(): Returns a new packaging set of the only direct parent element of all elements in the original packaging set; if a parameter is specified, the parameter is also a filter expression;
parents( ): Returns a new packaging set of ancestor elements of all elements in the original packaging set; if a parameter is specified, the parameter is also a filter expression;
prev(): Returns all unique previous sibling elements of the original packaging set elements A new packaging set; if a parameter is specified, then this parameter is also a filter expression;
prevAll(): Returns a new packaging set containing all previous sibling elements of the original packaging set element; if a parameter is specified, then This parameter is also a filter expression;
siblings(): Returns a new packaging set composed of all unique sibling elements in the original packaging set element; if a parameter is specified, then this parameter is also a filter expression;
find (String): Returns a new packaging set containing all elements in the original packaging set that match the passed selector expression, and the descendants of the elements in the original packaging set will also be passed into the new packaging set;
contains (text): Returns a new packaging set composed of elements containing the text String passed in as the text parameter;
is(String): If the packaging set contains elements matching String, then return true , otherwise it returns false;
clone(copyHandle): If the parameter passed in is true, then the event will be copied together, otherwise the event will not be copied and a new packaging set will be formed;
end(): In the jQuery command Called within the chain to return to the previous packaging set;
andSelf(): merge the two most recently generated packaging sets within the command chain;
##
The above is the detailed content of What is a jquery class array? How does it work?. For more information, please follow other related articles on the PHP Chinese website!