Differences between closest and parents in jQuery

Source: Internet
Author: User

Differences between closest and parents in jQuery

This article introduces the usage and differences between parents () and closest () in jquery. When you find that parents () in jquery deletes a parent element, it finds that it does not contain the root element, so closest () is used, and the effect is good. Let me introduce it to you in detail.

In jQuery, closest and parents refer to searching for some content of online elements in terms of performance, but if you may not understand it carefully, let's take a look at the differences between closest and parents.

1. Parent level

Parent () -- Unique parent element, which may be 0 or 1 element.

2. Ancestor

Parents (selected) -- all selected ancestors (excluding root elements), which may be 0, 1, or multiple elements.

Closest (selected) -- Unique selected ancestor, which may be 0 or 1 element.

The main differences between closest and parents are:

The former matches the current element and the latter matches the parent element;

The former searches up one by one until the matching elements are found and then stops. The latter searches up until the root elements are put into a temporary collection, filter with the given selector expression;

The former returns 0 or 1 element, and the latter may contain 0, 1, or multiple elements.

Closest is very useful for handling event delegation.

Summary

1. closest lookup begins with itself, and parents begins with the element parent level.

2. closest searches up. If you find a match, stop searching. parents searches for the root element and adds the matched element to the set.

3. closest returns a jquery object containing zero or one element. parents returns a jquery object containing zero or one or more elements.

The above is all the content of this article. I hope you will like it.

Note <>: For more exciting tutorials, please pay attention to the help house programming

Related Article
Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.