• JS功能实现


    轮播图
      <style>
        * {
          box-sizing: border-box;
        }
    
        .slider {
          width: 560px;
          height: 400px;
          overflow: hidden;
        }
    
        .slider-wrapper {
          width: 100%;
          height: 320px;
        }
    
        .slider-wrapper img {
          width: 100%;
          height: 100%;
          display: block;
        }
    
        .slider-footer {
          height: 80px;
          background-color: rgb(100, 67, 68);
          padding: 12px 12px 0 12px;
          position: relative;
        }
    
        .slider-footer .toggle {
          position: absolute;
          right: 0;
          top: 12px;
          display: flex;
        }
    
        .slider-footer .toggle button {
          margin-right: 12px;
          width: 28px;
          height: 28px;
          appearance: none;
          border: none;
          background: rgba(255, 255, 255, 0.1);
          color: #fff;
          border-radius: 4px;
          cursor: pointer;
        }
    
        .slider-footer .toggle button:hover {
          background: rgba(255, 255, 255, 0.2);
        }
    
        .slider-footer p {
          margin: 0;
          color: #fff;
          font-size: 18px;
          margin-bottom: 10px;
        }
    
        .slider-indicator {
          margin: 0;
          padding: 0;
          list-style: none;
          display: flex;
          align-items: center;
        }
    
        .slider-indicator li {
          width: 8px;
          height: 8px;
          margin: 4px;
          border-radius: 50%;
          background: #fff;
          opacity: 0.4;
          cursor: pointer;
        }
    
        .slider-indicator li.active {
          width: 12px;
          height: 12px;
          opacity: 1;
        }
      style>
    head>
    
    <body>
      <div class="slider">
        <div class="slider-wrapper">
          <img src="./images/slider01.jpg" alt="" />
        div>
        <div class="slider-footer">
          <p>对人类来说会不会太超前了?p>
          <ul class="slider-indicator">
            <li class="active">li>
            <li>li>
            <li>li>
            <li>li>
            <li>li>
            <li>li>
            <li>li>
            <li>li>
          ul>
          <div class="toggle">
            <button class="prev"><button>
            <button class="next">>button>
          div>
        div>
      div>
      <script>
        // 1. 初始数据
        const data = [
          { url: './images/slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },
          { url: './images/slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },
          { url: './images/slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },
          { url: './images/slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },
          { url: './images/slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },
          { url: './images/slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },
          { url: './images/slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },
          { url: './images/slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },
        ]
        // 获取元素
        const img = document.querySelector('.slider-wrapper img')
        const p = document.querySelector('.slider-footer p')
        const footer = document.querySelector('.slider-footer')
        // 1. 右按钮业务
        // 1.1 获取右侧按钮 
        const next = document.querySelector('.next')
        let i = 0  // 信号量 控制播放图片张数
        // 1.2 注册点击事件
    
        next.addEventListener('click', function () {
          // console.log(11)
          i++
          // 1.6判断条件  如果大于8 就复原为 0
          // if (i >= 8) {
          //   i = 0
          // }
          i = i >= data.length ? 0 : i
          // 1.3 得到对应的对象
          // console.log(data[i])
          // 调用函数
          toggle()
        })
    
        // 2. 左侧按钮业务
        // 2.1 获取左侧按钮 
        const prev = document.querySelector('.prev')
        // 1.2 注册点击事件
        prev.addEventListener('click', function () {
          i--
          // 判断条件  如果小于0  则爬到最后一张图片索引号是 7
          // if (i < 0) {
          //   i = 7
          // }
          i = i < 0 ? data.length - 1 : i
          // 1.3 得到对应的对象
          // console.log(data[i])
          // 调用函数
          toggle()
        })
    
        // 声明一个渲染的函数作为复用
        function toggle() {
          // 1.4 渲染对应的数据
          img.src = data[i].url
          p.innerHTML = data[i].title
          footer.style.backgroundColor = data[i].color
          // 1.5 更换小圆点    先移除原来的类名, 当前li再添加 这个 类名
          document.querySelector('.slider-indicator .active').classList.remove('active')
          document.querySelector(`.slider-indicator li:nth-child(${i + 1})`).classList.add('active')
        }
    
    
    
        // 3. 自动播放模块
        let timerId = setInterval(function () {
          // 利用js自动调用点击事件  click()  一定加小括号调用函数
          next.click()
        }, 1000)
    
    
        // 4. 鼠标经过大盒子,停止定时器
        const slider = document.querySelector('.slider')
        // 注册事件
        slider.addEventListener('mouseenter', function () {
          // 停止定时器
          clearInterval(timerId)
        })
    
        // 5. 鼠标离开大盒子,开启定时器
        // 注册事件
        slider.addEventListener('mouseleave', function () {
          // 停止定时器
          if (timerId) clearInterval(timerId)
          // 开启定时器
          timerId = setInterval(function () {
            // 利用js自动调用点击事件  click()  一定加小括号调用函数
            next.click()
          }, 1000)
        })
      script>
    body>
    
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    移动端轮播图
    DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Documenttitle>
      <link rel="stylesheet" href="./css/swiper.min.css">
      <style>
        .box {
          position: relative;
          width: 800px;
          height: 300px;
          background-color: pink;
          margin: 100px auto;
        }
    
        html,
        body {
          position: relative;
          height: 100%;
        }
    
        body {
          background: #eee;
          font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
          font-size: 14px;
          color: #000;
          margin: 0;
          padding: 0;
        }
    
        .swiper {
          overflow: hidden;
          width: 100%;
          height: 100%;
        }
    
        .swiper-slide {
          text-align: center;
          font-size: 18px;
          background: #fff;
    
          /* Center slide text vertically */
          display: -webkit-box;
          display: -ms-flexbox;
          display: -webkit-flex;
          display: flex;
          -webkit-box-pack: center;
          -ms-flex-pack: center;
          -webkit-justify-content: center;
          justify-content: center;
          -webkit-box-align: center;
          -ms-flex-align: center;
          -webkit-align-items: center;
          align-items: center;
        }
    
        .swiper-slide img {
          display: block;
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      style>
    
    head>
    
    <body>
      <div class="box">
        
        <div class="swiper mySwiper">
          <div class="swiper-wrapper">
            <div class="swiper-slide">Slide 1div>
            <div class="swiper-slide">Slide 2div>
            <div class="swiper-slide">Slide 3div>
            <div class="swiper-slide">Slide 4div>
            <div class="swiper-slide">Slide 5div>
            <div class="swiper-slide">Slide 6div>
            <div class="swiper-slide">Slide 7div>
            <div class="swiper-slide">Slide 8div>
            <div class="swiper-slide">Slide 9div>
          div>
          <div class="swiper-pagination">div>
        div>
    
    
      div>
      <script src="./js/swiper.min.js">script>
      <script>
        var swiper = new Swiper(".mySwiper", {
          // 小圆点
          pagination: {
            el: ".swiper-pagination",
          },
          // 自动播放
          autoplay: {
            delay: 1000,//1秒切换一次
            disableOnInteraction: false,  // 鼠标点击 触摸之后,自动继续播放
          },
          // 可以键盘控制
          keyboard: {
            enabled: true,
            onlyInViewport: true,
          },
        });
      script>
    body>
    
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    按下回车发表评论
      <style>
        .wrapper {
          min-width: 400px;
          max-width: 800px;
          display: flex;
          justify-content: flex-end;
        }
    
        .avatar {
          width: 48px;
          height: 48px;
          border-radius: 50%;
          overflow: hidden;
          background: url(./images/avatar.jpg) no-repeat center / cover;
          margin-right: 20px;
        }
    
        .wrapper textarea {
          outline: none;
          border-color: transparent;
          resize: none;
          background: #f5f5f5;
          border-radius: 4px;
          flex: 1;
          padding: 10px;
          transition: all 0.5s;
          height: 30px;
        }
    
        .wrapper textarea:focus {
          border-color: #e4e4e4;
          background: #fff;
          height: 50px;
        }
    
        .wrapper button {
          background: #00aeec;
          color: #fff;
          border: none;
          border-radius: 4px;
          margin-left: 10px;
          width: 70px;
          cursor: pointer;
        }
    
        .wrapper .total {
          margin-right: 80px;
          color: #999;
          margin-top: 5px;
          opacity: 0;
          transition: all 0.5s;
        }
    
        .list {
          min-width: 400px;
          max-width: 800px;
          display: flex;
        }
    
        .list .item {
          width: 100%;
          display: flex;
        }
    
        .list .item .info {
          flex: 1;
          border-bottom: 1px dashed #e4e4e4;
          padding-bottom: 10px;
        }
    
        .list .item p {
          margin: 0;
        }
    
        .list .item .name {
          color: #FB7299;
          font-size: 14px;
          font-weight: bold;
        }
    
        .list .item .text {
          color: #333;
          padding: 10px 0;
        }
    
        .list .item .time {
          color: #999;
          font-size: 12px;
        }
      style>
    head>
    
    <body>
      <div class="wrapper">
        <i class="avatar">i>
        <textarea id="tx" placeholder="发一条友善的评论" rows="2" maxlength="200">textarea>
        <button>发布button>
      div>
      <div class="wrapper">
        <span class="total">0/200字span>
      div>
      <div class="list">
        <div class="item" style="display: none;">
          <i class="avatar">i>
          <div class="info">
            <p class="name">清风徐来p>
            <p class="text">大家都辛苦啦,感谢各位大大的努力,能圆满完成真是太好了[笑哭][支持]p>
            <p class="time">2022-10-10 20:29:21p>
          div>
        div>
      div>
      <script>
        const tx = document.querySelector('#tx')
        const total = document.querySelector('.total')
        const item = document.querySelector('.item')
        const text = document.querySelector('.text')
        // 1. 当我们文本域获得了焦点,就让 total 显示出来
        tx.addEventListener('focus', function () {
          total.style.opacity = 1
        })
        // 2. 当我们文本域失去了焦点,就让 total 隐藏出来
        tx.addEventListener('blur', function () {
          total.style.opacity = 0
        })
        // 3. 检测用户输入
        tx.addEventListener('input', function () {
          // console.log(tx.value.length)  得到输入的长度
          total.innerHTML = `${tx.value.length}/200字`
        })
    
        // 4. 按下回车发布评论
        tx.addEventListener('keyup', function (e) {
          // 只有按下的是回车键,才会触发
          // console.log(e.key)
          if (e.key === 'Enter') {
            // 如果用户输入的不为空就显示和打印
            if (tx.value.trim()) {
              // console.log(11)
              item.style.display = 'block'
              // console.log(tx.value)  // 用户输入的内容
              text.innerHTML = tx.value
    
            }
            // 等我们按下回车,结束,清空文本域
            tx.value = ''
            // 按下回车之后,就要把 字符统计 复原
            total.innerHTML = '0/200字'
          }
    
        })
      script>
    body>
    
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    tab栏切换
      <style>
        * {
          margin: 0;
          padding: 0;
        }
    
        .tab {
          width: 590px;
          height: 340px;
          margin: 20px;
          border: 1px solid #e4e4e4;
        }
    
        .tab-nav {
          width: 100%;
          height: 60px;
          line-height: 60px;
          display: flex;
          justify-content: space-between;
        }
    
        .tab-nav h3 {
          font-size: 24px;
          font-weight: normal;
          margin-left: 20px;
        }
    
        .tab-nav ul {
          list-style: none;
          display: flex;
          justify-content: flex-end;
        }
    
        .tab-nav ul li {
          margin: 0 20px;
          font-size: 14px;
        }
    
        .tab-nav ul li a {
          text-decoration: none;
          border-bottom: 2px solid transparent;
          color: #333;
        }
    
        .tab-nav ul li a.active {
          border-color: #e1251b;
          color: #e1251b;
        }
    
        .tab-content {
          padding: 0 16px;
        }
    
        .tab-content .item {
          display: none;
        }
    
        .tab-content .item.active {
          display: block;
        }
      style>
    head>
    
    <body>
      <div class="tab">
        <div class="tab-nav">
          <h3>每日特价h3>
          <ul>
            <li><a class="active" href="javascript:;">精选a>li>
            <li><a href="javascript:;">美食a>li>
            <li><a href="javascript:;">百货a>li>
            <li><a href="javascript:;">个护a>li>
            <li><a href="javascript:;">预告a>li>
          ul>
        div>
        <div class="tab-content">
          <div class="item active"><img src="./images/tab00.png" alt="" />div>
          <div class="item"><img src="./images/tab01.png" alt="" />div>
          <div class="item"><img src="./images/tab02.png" alt="" />div>
          <div class="item"><img src="./images/tab03.png" alt="" />div>
          <div class="item"><img src="./images/tab04.png" alt="" />div>
        div>
      div>
      <script>
        // 1. a 模块制作 要给 5个链接绑定鼠标经过事件
        // 1.1 获取 a 元素 
        const as = document.querySelectorAll('.tab-nav a')
        // console.log(as) 
        for (let i = 0; i < as.length; i++) {
          // console.log(as[i])
          // 要给 5个链接绑定鼠标经过事件
          as[i].addEventListener('mouseenter', function () {
            // console.log('鼠标经过')
            // 排他思想  
            // 干掉别人 移除类active
            document.querySelector('.tab-nav .active').classList.remove('active')
            // 我登基 我添加类 active  this 当前的那个 a 
            this.classList.add('active')
    
            // 下面5个大盒子 一一对应  .item 
            // 干掉别人
            document.querySelector('.tab-content .active').classList.remove('active')
            // 对应序号的那个 item 显示 添加 active 类
            document.querySelector(`.tab-content .item:nth-child(${i + 1})`).classList.add('active')
    
          })
        }
      script>
    body>
    
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
      <style>
        * {
          margin: 0;
          padding: 0;
        }
    
        .tab {
          width: 590px;
          height: 340px;
          margin: 20px;
          border: 1px solid #e4e4e4;
        }
    
        .tab-nav {
          width: 100%;
          height: 60px;
          line-height: 60px;
          display: flex;
          justify-content: space-between;
        }
    
        .tab-nav h3 {
          font-size: 24px;
          font-weight: normal;
          margin-left: 20px;
        }
    
        .tab-nav ul {
          list-style: none;
          display: flex;
          justify-content: flex-end;
        }
    
        .tab-nav ul li {
          margin: 0 20px;
          font-size: 14px;
        }
    
        .tab-nav ul li a {
          text-decoration: none;
          border-bottom: 2px solid transparent;
          color: #333;
        }
    
        .tab-nav ul li a.active {
          border-color: #e1251b;
          color: #e1251b;
        }
    
        .tab-content {
          padding: 0 16px;
        }
    
        .tab-content .item {
          display: none;
        }
    
        .tab-content .item.active {
          display: block;
        }
      style>
    head>
    
    <body>
      <div class="tab">
        <div class="tab-nav">
          <h3>每日特价h3>
          <ul>
            <li><a class="active" href="javascript:;" data-id="0">精选a>li>
            <li><a href="javascript:;" data-id="1">美食a>li>
            <li><a href="javascript:;" data-id="2">百货a>li>
            <li><a href="javascript:;" data-id="3">个护a>li>
            <li><a href="javascript:;" data-id="4">预告a>li>
          ul>
        div>
        <div class="tab-content">
          <div class="item active"><img src="./images/tab00.png" alt="" />div>
          <div class="item"><img src="./images/tab01.png" alt="" />div>
          <div class="item"><img src="./images/tab02.png" alt="" />div>
          <div class="item"><img src="./images/tab03.png" alt="" />div>
          <div class="item"><img src="./images/tab04.png" alt="" />div>
        div>
      div>
      <script>
        // 采取事件委托的形式 tab栏切换
        // 1. 获取 ul 父元素 因为 ul只有一个
        const ul = document.querySelector('.tab-nav ul')
        // 获取 5个 item 
        const items = document.querySelectorAll('.tab-content .item')
        // 2. 添加事件
        ul.addEventListener('click', function (e) {
          // console.log(e.target)  // e.target是我们点击的对象
          // 我们只有点击了 a 才会 进行 添加类和删除类操作 
          // console.log(e.target.tagName)  // e.target.tagName 点击那个对象的 标签名
          if (e.target.tagName === 'A') {
            // console.log('我选的是a')
            // 排他思想 ,先移除原来的active  
            document.querySelector('.tab-nav .active').classList.remove('active')
            //当前元素添加 active  是 e.target
            // this 指向ul 不能用this 
            e.target.classList.add('active')
    
            // 下面大盒子模块
            // console.log(e.target.dataset.id)
            const i = +e.target.dataset.id
            // 排他思想 ,先移除原来的active 
            document.querySelector('.tab-content .active').classList.remove('active')
            // 对应的大盒子 添加 active 
            // document.querySelector(`.tab-content .item:nth-child(${i + 1})`).classList.add('active')
            items[i].classList.add('active')
          }
        })
      script>
    body>
    
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    全选按钮
      <style>
        * {
          margin: 0;
          padding: 0;
        }
    
        table {
          border-collapse: collapse;
          border-spacing: 0;
          border: 1px solid #c0c0c0;
          width: 500px;
          margin: 100px auto;
          text-align: center;
        }
    
        th {
          background-color: #09c;
          font: bold 16px "微软雅黑";
          color: #fff;
          height: 24px;
        }
    
        td {
          border: 1px solid #d0d0d0;
          color: #404060;
          padding: 10px;
        }
    
        .allCheck {
          width: 80px;
        }
      style>
    head>
    
    <body>
      <table>
        <tr>
          <th class="allCheck">
            <input type="checkbox" name="" id="checkAll"> <span class="all">全选span>
          th>
          <th>商品th>
          <th>商家th>
          <th>价格th>
        tr>
        <tr>
          <td>
            <input type="checkbox" name="check" class="ck">
          td>
          <td>小米手机td>
          <td>小米td>
          <td>¥1999td>
        tr>
        <tr>
          <td>
            <input type="checkbox" name="check" class="ck">
          td>
          <td>小米净水器td>
          <td>小米td>
          <td>¥4999td>
        tr>
        <tr>
          <td>
            <input type="checkbox" name="check" class="ck">
          td>
          <td>小米电视td>
          <td>小米td>
          <td>¥5999td>
        tr>
      table>
      <script>
        // 1. 获取大复选框
        const checkAll = document.querySelector('#checkAll')
        // 2. 获取所有的小复选框
        const cks = document.querySelectorAll('.ck')
        // 3. 点击大复选框  注册事件
        checkAll.addEventListener('click', function () {
          // 得到当前大复选框的选中状态
          // console.log(checkAll.checked)  // 得到 是 true 或者是 false
          // 4. 遍历所有的小复选框 让小复选框的checked  =  大复选框的 checked
          for (let i = 0; i < cks.length; i++) {
            cks[i].checked = this.checked
          }
        })
        // 5. 小复选框控制大复选框
    
        for (let i = 0; i < cks.length; i++) {
          // 5.1 给所有的小复选框添加点击事件
          cks[i].addEventListener('click', function () {
            // 判断选中的小复选框个数 是不是等于  总的小复选框个数
            // 一定要写到点击里面,因为每次要获得最新的个数
            // console.log(document.querySelectorAll('.ck:checked').length)
            // console.log(document.querySelectorAll('.ck:checked').length === cks.length)
            checkAll.checked = document.querySelectorAll('.ck:checked').length === cks.length
          })
        }
      script>
    body>
    
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
  • 相关阅读:
    STM32WB55开发(1)----套件概述
    MySQL数据库管理(三)索引
    【Java-----IO流(一)之字节流详解】
    065:vue+openlayers显示不同颜色point(示例代码)
    ZigBee 3.0理论教程-通用-1-10:安全加密-应用子层(APS)安全
    数字化转型道阻且长,如何迈好关键的第一步
    什么是bean的自动装配,有哪些方式?
    MQTT例程
    【无标题】
    k8s根ca证书最多10年调整为100年或任意时间
  • 原文地址:https://blog.csdn.net/qq_44317018/article/details/134170590