• 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.com/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.