Analysis of the difference between closest and parents in jquery

Source: Internet
Author: User

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

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.