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.