Home >Web Front-end >Front-end Q&A >How to use jquery's parent method
In jquery, the parent() method is used to return the direct parent element of the selected element. The returned result is an element set containing the unique parent element of all matching elements. Parameters can be set to specify narrowed search Parent element range, the syntax is "$(selector).parent(filter)".
The operating environment of this tutorial: windows10 system, jquery1.10.2 version, Dell G3 computer.
Returns the direct parent element of the selected element
parent() method:
parent([expr]): Gets a set of elements containing the unique parent element of all matching elements.
The syntax is:
$(selector).parent(filter)
filter is optional. Specifies a selector expression that narrows the search for parent elements.
The example is as follows:
<html> <head> <meta charset="utf-8"> <title>123</title> <style> .ancestors *{ display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px; } </style> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("span").parent().css({"color":"red","border":"2px solid red"}); }); </script> </head> <body class="ancestors">body (曾曾祖父节点) <div style="width:500px;">div (曾祖父节点) <ul>ul (祖父节点) <li>li (直接父节点) <span>span</span> </li> </ul> </div> </body> </html>
Output result:
##Related video tutorial recommendation:The above is the detailed content of How to use jquery's parent method. For more information, please follow other related articles on the PHP Chinese website!