Home > Web Front-end > JS Tutorial > body text

Analysis of the difference between parents() and parent() in jQuery_jquery

WBOY
Release: 2016-05-16 16:32:57
Original
1399 people have browsed it

This article analyzes the difference between parents() and parent() in jQuery with an example and shares it with you for your reference. The specific analysis is as follows:

In fact, in jQuery, the concept of function or selector is very easy to understand. As long as you are proficient enough in the API manual, you can easily distinguish it. The same is actually true for the function in the title, but let’s do some simple ones here. Introduction, after all, this may be more convenient.

1.parents() function:

This function can get all parent elements of the matching element, code example:

Copy code The code is as follows:
$(".mayi").parents().css(" color","red");

The above code can set the font color of all parent elements of all elements whose class attribute value is "mayi" to red.
See a complete code example:

Copy code The code is as follows:





Count the number of selected checkboxes-Script Home





Script House 1

Script House One by One
Script House welcomes you




The above code can set the font color in all parent elements of the span element to red.

2.parent() function:

This function can get the first-level parent elements of all matching elements, not all parent elements.

Copy code The code is as follows:
$(".mayi").parents().css(" color","red");

The above code can set the font color in the first-level parent element of the element whose class attribute value is mayi to red.
Take a look at the complete code:

Copy code The code is as follows:





Count the number of selected checkboxes-Script Home





Script House 1

Script House One by One
Script House welcomes you




The above code can only set the font color of "Script Home One One" in the first-level parent element of the span element to red.

The biggest difference between these two functions is the return of the parent element. You can also feel it from the name of the function. With s, you can get all the parent elements. Without s, you can only get the first-level parent element. It is very simple. Here No more introduction, interested friends can refer to related articles.

I hope this article will be helpful to everyone’s jQuery programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template