jQuery - méthode parent ([selector])

La description

le parent( [selector] )La méthode obtient le parent direct d'un élément. S'il est appelé sur un ensemble d'éléments, parent renvoie un ensemble de leurs éléments parents directs uniques.

Syntaxe

Voici la syntaxe simple pour utiliser cette méthode -

selector.parent( [selector] )

Paramètres

Voici la description de tous les paramètres utilisés par cette méthode -

  • selector - Il s'agit d'un sélecteur facultatif avec lequel filtrer le parent.

Exemple

Voici un exemple simple montrant l'utilisation de cette méthode.

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function(){
            $("p").parent().addClass('hilight');
         });
      </script>
		
      <style>
         .hilight { background:yellow; }
      </style>
   </head>
	
   <body>
      <scan>Top Element</scan>
      <div>
         <div>sibling<div>child</div></div>
         <p>sibling</p>
         <scan>sibling</scan>
      </div>
   </body>
</html>

Cela produira le résultat suivant -

Exemple

Voici un exemple simple montrant l'utilisation de cette méthode.

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function(){
            $("p").parent().addClass('hilight');
         });
      </script>
		
      <style>
         .hilight { background:yellow; }
      </style>
   </head>
	
   <body>
      <scan>Top Element</scan>
      <div class = "hilight">
         <div>sibling<div>child</div></div>
         <p>sibling</p>
         <scan>sibling</scan>
      <div>
   </body>
</html>

Cela produira le résultat suivant -

jquery-traversing.htm