• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 类选择器

    选择给定样式类名的所有元素。

    jQuery(".class")

    class:一个用来查找的类名。一个元素可以有多个类;其中只有一个必须匹配。

    对于类选择器,如果浏览器支持,jQuery使用JavaScript的原生getElementsByClassName()函数来实现。

    例子

    找到该元素的类名为“myclass”。

    <!DOCTYPE html>
    <html>
    <head>
      <style>
      div,span {
        width: 100px;
        height: 40px;
        float:left;
        padding: 10px;
        margin: 10px;
        background-color: #EEEEEE;
      }
      </style>
      <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.js"></script>
    </head>
    <body>
      <div class="notMe">div class="notMe"</div>
     
      <div class="myClass">div class="myClass"</div>
      <span class="myClass">span class="myClass"</span>
    <script>$(".myClass").css("border","3px solid red");</script>
     
    </body>
    </html>
    

    查找的元素都“的myclass”和“otherclass的的”类。

    <!DOCTYPE html>
    <html>
    <head>
      <style>
      div,span {
        width: 100px;
        height: 40px;
        float:left;
        padding: 10px;
        margin: 10px;
        background-color: #EEEEEE;
      }
      </style>
      <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.js"></script>
    </head>
    <body>
      <div class="myclass">div class="notMe"</div>
     
      <div class="myclass otherclass">div class="myClass"</div>
      <span class="myclass otherclass">span class="myClass"</span>
    <script>$(".myclass.otherclass").css("border","13px solid red");</script>
     
    </body>
    </html>
    

    上篇:通配符选择器

    下篇:id选择器