jQuery - 多元素選擇器



描述

此多元素選擇器選擇所有指定選擇器 E、F 或 G 的組合結果。

您可以指定任意數量的選擇器以組合成單個結果。此處 jQuery 物件中 DOM 元素的順序不一定相同。

語法

以下是使用此選擇器的簡單語法:

$('E, F, G,....')

引數

以下是此選擇器使用所有引數的描述:

  • E − 任何有效的選擇器

  • F − 任何有效的選擇器

  • G − 任何有效的選擇器

  • ....

返回值

與任何其他 jQuery 選擇器一樣,此選擇器也返回一個填充了找到元素的陣列。

示例

  • $('div, p') − 選擇與divp 匹配的所有元素。

  • $('p strong, .myclass') − 選擇與p 元素的後代strong 元素匹配的所有元素,以及所有具有myclass 類的元素。

  • $('p strong, #myid') − 選擇與p 元素的後代strong 元素匹配的單個元素,以及 id 為myid 的元素。

以下示例將選擇具有類 ID big 的元素和具有 ID div3 的元素,並將黃色應用於其背景:

<html>
   <head>
      <title>The Selecter Example</title>
      <script type = "text/javascript" 
         src = "https://tutorialspoint.tw/jquery/jquery-3.6.0.js">
      </script>
   
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $(".big, #div3").css("background-color", "yellow");
         });
      </script>
   </head>
	
   <body>
      <div class = "big" id = "div1">
         <p>This is first division of the DOM.</p>
      </div>

      <div class = "medium" id = "div2">
         <p>This is second division of the DOM.</p>
      </div>

      <div class = "small" id = "div3">
         <p>This is third division of the DOM</p>
      </div>
   </body>
</html>

這將產生以下結果:

jquery-selectors.htm
廣告
© . All rights reserved.