jQuery选择器 之 基本选择器

说明

  • 标签选择器(也叫元素选择器):语法为$(‘标签名称’) ,获得所有匹配标签名称的元素。
  • id选择器:语法为$(‘#id的属性值’) ,获得与指定id属性值匹配的元素。
  • 类选择器:语法为$(‘.class的属性值’) ,获得与指定的class属性值匹配的元素。

示例

使用dom对象获取元素

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

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="one" style="width: 200px; height: 200px; background-color: skyblue;">这是div中的内容</div>
  <input type="button" id="b1" value="点击此按钮,改变div的背景色">

  <script>
    // 使用dom对象
    const b1Ele = document.getElementById('b1')
    b1Ele.onclick = function () {
      document.getElementById('one').style.backgroundColor = 'pink'
    }
  </script>
</body>

</html>

在这里插入图片描述

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

使用jQuery的id选择器

<!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>
</head>

<body>
  <input type="button" id="b1" value="点击此按钮,改变div的背景色"><br><br>
  <div id="one" style="width: 200px; height: 200px; background-color: skyblue;">这是div中的内容</div>

  <script>
    const $b1Ele = $('#b1')
    // 绑定鼠标点击事件
    $b1Ele.click(function () {
      $('#one').css('background-color', 'red')
    })
  </script>
</body>

</html>

在这里插入图片描述

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

使用jQuery的标签选择器

<!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>
</head>

<body>
  <input type="button" id="b1" value="点击此按钮,改变div的背景色"><br><br>
  <div style="width: 200px; height: 200px; background-color: skyblue;">这是div中的内容</div>
  <div style="width: 200px; height: 200px; background-color: pink;">这是div中的内容</div>

  <script>
    const $b1Ele = $('#b1')
    // 绑定鼠标点击事件
    $b1Ele.click(function () {
      $('div').css('background-color', 'red')
    })
  </script>
</body>

</html>

在这里插入图片描述

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

使用jQuery的类选择器

<!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>
</head>

<body>
  <input type="button" id="b1" value="点击此按钮,改变div的背景色"><br><br>
  <div class="one" style="width: 200px; height: 200px; background-color: skyblue;">这是div中的内容</div>
  <div style="width: 200px; height: 200px; background-color: pink;">这是div中的内容</div>

  <script>
    const $b1Ele = $('#b1')
    // 绑定鼠标点击事件
    $b1Ele.click(function () {
      $('.one').css('background-color', 'red')
    })
  </script>
</body>

</html>

在这里插入图片描述

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

使用jQuery的多选择器

<!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>
</head>

<body>
  <input type="button" id="b1" value="点击此按钮,改变特定元素的背景色"><br><br>
  <div id="one" style="width: 200px; height: 200px; background-color: skyblue;">这是div中的内容</div>
  <div style="width: 200px; height: 200px; background-color: pink;">这是div中的内容</div>
  <span style="display: inline-block; width: 200px; height: 200px; background-color: green;">这是span元素中的内容</span>

  <script>
    const $b1Ele = $('#b1')
    // 绑定鼠标点击事件
    $b1Ele.click(function () {
      $('span, #one').css('background-color', 'red')
    })
  </script>
</body>

</html>

在这里插入图片描述

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值