Home  >  Article  >  Web Front-end  >  Usage example of parent() method in jQuery_jquery

Usage example of parent() method in jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 16:21:411207browse

The example in this article describes the usage of the parent() method in jQuery. Share it with everyone for your reference. The specific analysis is as follows:

This method obtains the immediate parent element of each element in the matching element set, that is, the first-level parent element, rather than all ancestor elements.
The obtained set of parent elements can also be filtered using expressions.

Grammar structure:

Copy code The code is as follows:
$(selector).parent(expr)

Parameter list:
参数 描述
expr 可选。用来筛选的表达式

Example code:

Example 1:

Copy code The code is as follows:






parent() function-Script Home






I am the p element





  • I am the p element






Get the respective immediate parent elements of the p element and set their border color to red.

Example 2:

Copy code The code is as follows:






parent() function-Script Home






I am the p element





  • I am the p element






Get the div parent element immediately adjacent to the p element and set its border color to red.

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

Statement:
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