This article introduces jquery parents () and closest () usage and difference introduction, in jquery parents () find the parent element deletion, found that it does not contain the root element, so the use of closest (), the effect is good, Let me give you a specific introduction
jquery closest and parents from the performance is said to find some content of the elements of the line, but if you are careful may not understand, let's look at the difference between closest and parents.
1. Parent level
Parent ()-The unique parent element, which may be 0 or 1 elements.
2. Ancestors
Parents (selected)--All selected ancestors (without the root element), may be 0, one or more elements.
Closest (selected)-The only selected ancestor, may be 0 or 1 elements.
The main differences between closest and parents are:
The former matches the search from the current element, the latter starting from the parent element.
The former is searched up until the matching element is found, the latter is searched up until the root element, and then the elements are placed in a temporary set and then filtered with the given selector expression;
The former returns 0 or 1 elements, which may contain 0, 1, or more elements.
Closest is useful for handling event delegates.
Summarize
1. Closest lookup begins on itself, parents starts at element parent
2, closest up, know to find a match to stop looking, parents always find the root element, and add matching elements to the collection
3. Closest returns a JQuery object that contains 0 or one element, parents returns a jquery object that contains 0 or more elements
The above mentioned is the entire content of this article, I hope you can enjoy.
Note < : More Wonderful tutorials please focus on the triple Programming