jQuery 中的 .Class函数

首页 / jQuery入门教程 / jQuery 中的 .Class函数

元素类选择器选择与给定元素类匹配的所有元素。

.Class - 语法

这是使用此选择器的简单语法-

$('.classid')
  • classid -这是文档中可用的classid。

.Class - 返回值

像任何其他jQueryselector一样,该选择器还返回一个填充有找到的元素的数组。

.Class - 示例

  • $('.big')            -  选择具有给定class='big' 的所有元素。

  • $('p.small')     -  选择具有给定<p class='small'></p> 的所有段落。

  • $('.big.small') -  选择所有class='big small' 的元素。

以下示例将选择所有类别为 .big 的部门,并将黄色应用于其背景-

<html>
   <head>
      <title>The Selecter 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() {
            /* This would select second division only*/
            $(".big").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>

这将产生以下输出-

This is first division of the DOM.

This is second division of the DOM.

This is third division of the DOM

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

编辑训练营 -〔总编室〕

Netty源码剖析与实战 -〔傅健〕

苏杰的产品创新课 -〔苏杰〕

Serverless入门课 -〔蒲松洋(秦粤)〕

TensorFlow 2项目进阶实战 -〔彭靖田〕

Web安全攻防实战 -〔王昊天〕

Spring编程常见错误50例 -〔傅健〕

深入C语言和程序运行原理 -〔于航〕

结构思考力 · 透过结构看表达 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)