Home >Web Front-end >JS Tutorial >How to find the parent class of a specified element in jquery

How to find the parent class of a specified element in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-12-24 15:24:425264browse

Jquery method to find the parent class of a specified element: 1. Use the [closest()] method to return the first ancestor of the selected element; 2. Use the [parent()] method to return the selected element The element's immediate parent.

How to find the parent class of a specified element in jquery

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, DELL G3 computer.

Recommended: jquery video tutorial

##jquery method to find the parent class of a specified element:

In jquery, you can use the closest() method and parent() method to find the parent class of a specified element.

  • closest() method is used to return the first ancestor of the selected element.

  • parent() method is used to return the direct parent element of the selected element.

Example 1: Use the closest() method to get the first matching ancestor of an element.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body style="text-align:center;"> 
    <p style= "font-size: 17px; font-weight: bold;">点击按钮,查看结果</p> 
      
    <div class="parent"> 
        <div class="child"></div> 
    </div> 
      
    <button>点击</button> 
      
    <p id="DOWN" style="color: green; font-size: 24px; font-weight: bold;"> </p> 
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script> 
        $(&#39;button&#39;).on(&#39;click&#39;, function() { 
            var object = $(&#39;.child&#39;).closest(&#39;.parent&#39;); 
              
            if (object.length) { 
                $(&#39;#DOWN&#39;).text("className = &#39;.child&#39;" + ",parentName = &#39;.parent&#39;"); 
            }  
            else { 
                $(&#39;#DOWN&#39;).text("不存在父类"); 
            } 
        }); 
    </script> 
</body> 
  
</html>

Rendering:

How to find the parent class of a specified element in jquery

Example 2: Use the parent() method to get the direct parent element of the element

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<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("li").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>
<li>li (直接父节点)
<span>span</span>
</li>
</ul>   
</div>
</body>
</html>

Rendering:

How to find the parent class of a specified element in jquery

Related free learning recommendations: javascript(Video)

The above is the detailed content of How to find the parent class of a specified element in jquery. 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