Home >Web Front-end >Front-end Q&A >How to use jquery's parent method

How to use jquery's parent method

WBOY
WBOYOriginal
2022-03-16 14:42:442458browse

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)".

How to use jquery's parent method

The operating environment of this tutorial: windows10 system, jquery1.10.2 version, Dell G3 computer.

How to use jquery’s parent method

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:

How to use jquerys parent method

##Related video tutorial recommendation:

jQuery video tutorial

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!

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