jQuery选择器 之 基本过滤选择器

语法说明

https://api.jquery.com/category/selectors/basic-filter-selectors/

  • :first 选择器:选择第一个匹配的DOM元素,例如$('div:first')
  • :last 选择器:选择最后一个匹配的元素,例如$('div:last')
  • :not() 选择器:选择所有不匹配给定选择器的元素,例如$('div:not(".one")')
  • :even 选择器:选择偶数索引的元素(从零开始计数)例如$('div:even')
  • :odd 选择器:选择奇数索引的元素(从零开始计数),例如 $('div:odd')
  • :eq() 选择器:选择匹配集中索引为n的元素,例如$('div:eq(3)')
  • :gt() 选择器:选择匹配集中索引大于index的所有元素,例如$('div:gt(3)')
  • :lt() 选择器:选择匹配集中索引小于index的所有元素,例如$('div:lt(3)')
  • :header 选择器:选择所有标题元素,如h1、h2、h3等,例如$(':header')
  • :animated 选择器:选择在运行选择器时处于动画过程中的所有元素。

示例

:first 选择器

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="js/jquery-3.7.1.js"></script>
  <style>
    body {
      font-size: 24px;
    }

    #one {
      float: left;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    #two {
      margin-left: 20px;
      float: left;
      width: 300px;
      height: 300px;
      background-color: skyblue;
    }

    .mini {
      margin-top: 10px;
      width: 100px;
      height: 100px;
      background-color: blueviolet;
    }

    .one {
      float: left;
      margin-left: 20px;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    span {
      display: inline-block;
      margin-left: 20px;
      width: 200px;
      height: 200px;
      background-color: purple;
    }

    input {
      font-size: 30px;
      margin-bottom: 30px;
    }
  </style>
</head>

<body>
  <input type="button" id="b1" value="点击按钮,改变第一个div元素的背景色为红色"><br><br>
  <div id="one">div:id为one</div>
  <div id="two" title="test">div:id为two,title="test"
    <div class="mini">div:class为mini</div>
  </div>
  <div class="one" title="test02">div:class是one,title="test02"
    <div class="mini">div:class是mini</div>
    <div class="mini">div:class是mini</div>
  </div>
  <div class="one">div:class是one</div>
  <span>span</span>

  <script>
    $('#b1').click(function () {
      $('div:first').css('background-color', 'red')
    })
  </script>
</body>

</html>

在这里插入图片描述

点击上面按钮:
在这里插入图片描述

:last 选择器

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="js/jquery-3.7.1.js"></script>
  <style>
    body {
      font-size: 24px;
    }

    #one {
      float: left;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    #two {
      margin-left: 20px;
      float: left;
      width: 300px;
      height: 300px;
      background-color: skyblue;
    }

    .mini {
      margin-top: 10px;
      width: 100px;
      height: 100px;
      background-color: blueviolet;
    }

    .one {
      float: left;
      margin-left: 20px;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    span {
      display: inline-block;
      margin-left: 20px;
      width: 200px;
      height: 200px;
      background-color: purple;
    }

    input {
      font-size: 30px;
      margin-bottom: 30px;
    }
  </style>
</head>

<body>
  <input type="button" id="b1" value="点击按钮,改变最后一个div元素的背景色为红色"><br><br>
  <div id="one">div:id为one</div>
  <div id="two" title="test">div:id为two,title="test"
    <div class="mini">div:class为mini</div>
  </div>
  <div class="one" title="test02">div:class是one,title="test02"
    <div class="mini">div:class是mini</div>
    <div class="mini">div:class是mini</div>
  </div>
  <div class="one">div:class是one</div>
  <span>span</span>

  <script>
    $('#b1').click(function () {
      $('div:last').css('background-color', 'red')
    })
  </script>
</body>

</html>

在这里插入图片描述

点击上面按钮:
在这里插入图片描述

:not() 选择器

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="js/jquery-3.7.1.js"></script>
  <style>
    body {
      font-size: 24px;
    }

    #one {
      float: left;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    #two {
      margin-left: 20px;
      float: left;
      width: 300px;
      height: 300px;
      background-color: skyblue;
    }

    .mini {
      margin-top: 10px;
      width: 100px;
      height: 100px;
      background-color: blueviolet;
    }

    .one {
      float: left;
      margin-left: 20px;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    span {
      display: inline-block;
      margin-left: 20px;
      width: 200px;
      height: 200px;
      background-color: purple;
    }

    input {
      font-size: 30px;
      margin-bottom: 30px;
    }
  </style>
</head>

<body>
  <input type="button" id="b1" value="点击按钮,改变class不为one的所有div元素的背景色为红色"><br><br>
  <div id="one">div:id为one</div>
  <div id="two" title="test">div:id为two,title="test"
    <div class="mini">div:class为mini</div>
  </div>
  <div class="one" title="test02">div:class是one,title="test02"
    <div class="mini">div:class是mini</div>
    <div class="mini">div:class是mini</div>
  </div>
  <div class="one">div:class是one</div>
  <span>span</span>

  <script>
    $('#b1').click(function () {
      $('div:not(".one")').css('background-color', 'red')
    })
  </script>
</body>

</html>

在这里插入图片描述

点击上面按钮:
在这里插入图片描述

:even 选择器

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="js/jquery-3.7.1.js"></script>
  <style>
    body {
      font-size: 24px;
    }

    #one {
      float: left;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    #two {
      margin-left: 20px;
      float: left;
      width: 300px;
      height: 300px;
      background-color: skyblue;
    }

    .mini {
      margin-top: 10px;
      width: 100px;
      height: 100px;
      background-color: blueviolet;
    }

    .one {
      float: left;
      margin-left: 20px;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    span {
      display: inline-block;
      margin-left: 20px;
      width: 200px;
      height: 200px;
      background-color: purple;
    }

    input {
      font-size: 30px;
      margin-bottom: 30px;
    }
  </style>
</head>

<body>
  <input type="button" id="b1" value="点击按钮,改变索引值为偶数的div元素的背景色为红色"><br><br>
  <div id="one">div:id为one</div>
  <div id="two" title="test">div:id为two,title="test"
    <div class="mini">div:class为mini</div>
  </div>
  <div class="one" title="test02">div:class是one,title="test02"
    <div class="mini">div:class是mini</div>
    <div class="mini">div:class是mini</div>
  </div>
  <div class="one">div:class是one</div>
  <span>span</span>

  <script>
    $('#b1').click(function () {
      $('div:even').css('background-color', 'red')
    })
  </script>
</body>

</html>

在这里插入图片描述

点击上面按钮:
在这里插入图片描述

:odd 选择器

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="js/jquery-3.7.1.js"></script>
  <style>
    body {
      font-size: 24px;
    }

    #one {
      float: left;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    #two {
      margin-left: 20px;
      float: left;
      width: 300px;
      height: 300px;
      background-color: skyblue;
    }

    .mini {
      margin-top: 10px;
      width: 100px;
      height: 100px;
      background-color: blueviolet;
    }

    .one {
      float: left;
      margin-left: 20px;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    span {
      display: inline-block;
      margin-left: 20px;
      width: 200px;
      height: 200px;
      background-color: purple;
    }

    input {
      font-size: 30px;
      margin-bottom: 30px;
    }
  </style>
</head>

<body>
  <input type="button" id="b1" value="点击按钮,改变索引值为奇数的div元素的背景色为红色"><br><br>
  <div id="one">div:id为one</div>
  <div id="two" title="test">div:id为two,title="test"
    <div class="mini">div:class为mini</div>
  </div>
  <div class="one" title="test02">div:class是one,title="test02"
    <div class="mini">div:class是mini</div>
    <div class="mini">div:class是mini</div>
  </div>
  <div class="one">div:class是one</div>
  <span>span</span>

  <script>
    $('#b1').click(function () {
      $('div:odd').css('background-color', 'red')
    })
  </script>
</body>

</html>

在这里插入图片描述

点击上面按钮:
在这里插入图片描述

:gt() 选择器

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="js/jquery-3.7.1.js"></script>
  <style>
    body {
      font-size: 24px;
    }

    #one {
      float: left;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    #two {
      margin-left: 20px;
      float: left;
      width: 300px;
      height: 300px;
      background-color: skyblue;
    }

    .mini {
      margin-top: 10px;
      width: 100px;
      height: 100px;
      background-color: blueviolet;
    }

    .one {
      float: left;
      margin-left: 20px;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    span {
      display: inline-block;
      margin-left: 20px;
      width: 200px;
      height: 200px;
      background-color: purple;
    }

    input {
      font-size: 30px;
      margin-bottom: 30px;
    }
  </style>
</head>

<body>
  <input type="button" id="b1" value="点击按钮,改变索引值大于3的div元素的背景色为红色"><br><br>
  <div id="one">div:id为one</div>
  <div id="two" title="test">div:id为two,title="test"
    <div class="mini">div:class为mini</div>
  </div>
  <div class="one" title="test02">div:class是one,title="test02"
    <div class="mini">div:class是mini</div>
    <div class="mini">div:class是mini</div>
  </div>
  <div class="one">div:class是one</div>
  <span>span</span>

  <script>
    $('#b1').click(function () {
      $('div:gt(3)').css('background-color', 'red')
    })
  </script>
</body>

</html>

在这里插入图片描述

点击上面按钮:
在这里插入图片描述

:eq() 选择器

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="js/jquery-3.7.1.js"></script>
  <style>
    body {
      font-size: 24px;
    }

    #one {
      float: left;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    #two {
      margin-left: 20px;
      float: left;
      width: 300px;
      height: 300px;
      background-color: skyblue;
    }

    .mini {
      margin-top: 10px;
      width: 100px;
      height: 100px;
      background-color: blueviolet;
    }

    .one {
      float: left;
      margin-left: 20px;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    span {
      display: inline-block;
      margin-left: 20px;
      width: 200px;
      height: 200px;
      background-color: purple;
    }

    input {
      font-size: 30px;
      margin-bottom: 30px;
    }
  </style>
</head>

<body>
  <input type="button" id="b1" value="点击按钮,改变索引值等于3的div元素的背景色为红色"><br><br>
  <div id="one">div:id为one</div>
  <div id="two" title="test">div:id为two,title="test"
    <div class="mini">div:class为mini</div>
  </div>
  <div class="one" title="test02">div:class是one,title="test02"
    <div class="mini">div:class是mini</div>
    <div class="mini">div:class是mini</div>
  </div>
  <div class="one">div:class是one</div>
  <span>span</span>

  <script>
    $('#b1').click(function () {
      $('div:eq(3)').css('background-color', 'red')
    })
  </script>
</body>

</html>

在这里插入图片描述

点击上面按钮:
在这里插入图片描述

:lt() 选择器

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="js/jquery-3.7.1.js"></script>
  <style>
    body {
      font-size: 24px;
    }

    #one {
      float: left;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    #two {
      margin-left: 20px;
      float: left;
      width: 300px;
      height: 300px;
      background-color: skyblue;
    }

    .mini {
      margin-top: 10px;
      width: 100px;
      height: 100px;
      background-color: blueviolet;
    }

    .one {
      float: left;
      margin-left: 20px;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    span {
      display: inline-block;
      margin-left: 20px;
      width: 200px;
      height: 200px;
      background-color: purple;
    }

    input {
      font-size: 30px;
      margin-bottom: 30px;
    }
  </style>
</head>

<body>
  <input type="button" id="b1" value="点击按钮,改变索引值小于3的div元素的背景色为红色"><br><br>
  <div id="one">div:id为one</div>
  <div id="two" title="test">div:id为two,title="test"
    <div class="mini">div:class为mini</div>
  </div>
  <div class="one" title="test02">div:class是one,title="test02"
    <div class="mini">div:class是mini</div>
    <div class="mini">div:class是mini</div>
  </div>
  <div class="one">div:class是one</div>
  <span>span</span>

  <script>
    $('#b1').click(function () {
      $('div:lt(3)').css('background-color', 'red')
    })
  </script>
</body>

</html>

在这里插入图片描述

点击上面按钮:
在这里插入图片描述

:header 选择器

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="js/jquery-3.7.1.js"></script>
  <style>
    body {
      font-size: 24px;
    }

    #one {
      float: left;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    #two {
      margin-left: 20px;
      float: left;
      width: 300px;
      height: 300px;
      background-color: skyblue;
    }

    .mini {
      margin-top: 10px;
      width: 100px;
      height: 100px;
      background-color: blueviolet;
    }

    .one {
      float: left;
      margin-left: 20px;
      width: 300px;
      height: 300px;
      background-color: #979ccd;
    }

    span {
      display: inline-block;
      margin-left: 20px;
      width: 200px;
      height: 200px;
      background-color: purple;
    }

    input {
      font-size: 30px;
      margin-bottom: 30px;
    }
  </style>
</head>

<body>
  <input type="button" id="b1" value="点击按钮,改变所有的标题元素的背景色为红色"><br><br>
  <h1>h1的内容:千里之行,始于足下</h1>
  <h3>h3的内容:中秋节、国庆节的脚步越来越近了</h3>

  <div id="one">div:id为one</div>
  <div id="two" title="test">div:id为two,title="test"
    <div class="mini">div:class为mini</div>
  </div>
  <div class="one" title="test02">div:class是one,title="test02"
    <div class="mini">div:class是mini</div>
    <div class="mini">div:class是mini</div>
  </div>
  <div class="one">div:class是one</div>
  <span>span</span>

  <script>
    $('#b1').click(function () {
      $(':header').css('background-color', 'red')
    })
  </script>
</body>

</html>

在这里插入图片描述

点击上面按钮:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值