Differences between the parents () and parent () functions of jQuery

Source: Internet
Author: User
Tags api manual

Differences between the parents () function and the parent () function of jQuery:
In fact, in jQuery, the concept of a function or selector is easy to understand. As long as you are familiar with the API manual, it is easy to distinguish between functions in the title, however, we will make some simple introductions here. After all, this may be more convenient.
I. parents () function:
This function can obtain all the parent elements of the matching element. Sample Code:

$(".mayi").parents().css("color","red");

The above Code sets the font color of all parent elements of all elements whose class attribute value is "mayi" to red.
Take a look at the complete code example:

<! DOCTYPE html> 

The above Code sets the font color of all parent elements of the span element to red.
Ii. parent () function:
This function can obtain the first-level parent elements of all matching elements, rather than all parent elements.

$(".mayi").parents().css("color","red");

The above Code sets the font color in the first-level parent element of an element whose class property value is mayi to red.
Take a look at the complete code:

<! DOCTYPE html> 

The above code can only set the font color of "ant tribe one by one" in the first-level parent element of the span element to red.
The biggest difference between the two functions is the return of the parent element, which can be noticed by the function name. With s, all the parent elements can be obtained. Without s, only the first-level parent elements can be obtained, it is very simple. I will not discuss it here. For more information, see.
Related reading:
1. For details about the css () function, see the css () method section of jQuery.
2. For details about the parent () function, refer to the parent () method section of jQuery.
3. For details about the parents () function, see section 1 of parents () method of jQuery.

 

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.