jQuery 多类选择器用法介绍

2021年3月13日15:28:41 发表评论 1,002 次浏览

.class选择器在jQuery中用于选择多个类。

语法如下:

$(".class1, .class2, .class3, ...")

参数:

  • 类:需要此参数来指定要选择的元素的类别。

示例1:

<!DOCTYPE html>
< html >
  
< head >
     < script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" >
   </ script >
     
   < script >
         $(document).ready(function() {
          $(".geeks, .for, .lsbin").css(
               "background-color", "green");
         });
     </ script >
</ head >
  
< body >
     < center >
         < h1 class = "geeks" >
           Welcome to lsbin
       </ h1 >
        
         < p >Geeks1</ p >
         < p class = "for" >Geeks2</ p >
         < p >Geeks3</ p >
         < p class = "lsbin" >
           Geeks3
       </ p >
     </ center >
</ body >
  
</ html >

输出如下:

jQuery |多类选择器1

示例2:

<!DOCTYPE html>
< html >
  
< head >
     < script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" >
   </ script >
  
   < script >
         $(document).ready(function() {
             $(".c1, .c2").css(
               "background-color", "green");
         });
     </ script >
</ head >
  
< body >
     < center >
         < h1 class = "geeks" >
           Welcome to lsbin
       </ h1 >
        
         < p class = "c1" >Geeks1</ p >
         < p class = "for" >Geeks2</ p >
         < p class = "c2" >Geeks3</ p >
         < p class =
            "lsbin" >
           Geeks3
       </ p >
     </ center >
</ body >
  
</ html >

输出如下:

jQuery |多类选择器2

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: