• jQuery Video Tutorials

jQuery - 元素名稱選擇器



描述

元素選擇器選擇所有標籤名稱為 T 的元素。

語法

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

$('tagname')

引數

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

  • tagname − 任何標準的 HTML 標籤名稱,例如 div、p、em、img、li 等。

返回值

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

示例

  • $('p') − 選擇文件中所有標籤名稱為 p 的元素。

  • $('div') − 選擇文件中所有標籤名稱為 div 的元素。

以下示例將選擇所有分割槽並將黃色應用於其背景:

<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() {
            /* This would select all the divisions */
            $("div").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.