Jquery-traversal-parent

提供:Dev Guides
移動先:案内検索

jQuery-parent([セレクタ])メソッド

説明

  • parent([selector])*メソッドは、要素の直接の親を取得します。 一連の要素に対して呼び出された場合、parentは一意の直接の親要素のセットを返します。

構文

このメソッドを使用するための簡単な構文は次のとおりです-

selector.parent( [selector] )

パラメーター

これは、この方法で使用されるすべてのパラメータの説明です-

  • selector -これは、親をフィルターするオプションのセレクターです。

以下は、このメソッドの使用方法を示す簡単な例です。

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

これにより、次の結果が生成されます–

以下は、このメソッドの使用方法を示す簡単な例です。

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

これにより、次の結果が生成されます–