• js实现全选按钮


    目录

    html代码

    css代码

    js代码

    完整代码


    html代码

    先把整体结构样式写出来

    1. <table>
    2. <thead>
    3. <tr>
    4. <th class="allCheck">
    5. <input type="checkbox" name="" id="checkAll" />
    6. <span class="all">全选span>
    7. th>
    8. <th>商品th>
    9. <th>商家th>
    10. <th>价格th>
    11. tr>
    12. thead>
    13. <tbody class="goods">tbody>
    14. table>

    css代码

    对表格添加样式,使其呈现在页面美观

    js代码

    首先要先渲染页面,把整体样式通过js呈现在页面上

    根据需求写js代码

    点击全选按钮 ,其他按钮都被选中

    绑定事件,让所有商品状态与全选按钮状态同步

    单击每个商品 设置全选按钮状态    

     单击某个商品   所有商品都选中,全选按钮选中,只要有一个未选中全选按钮取消  

    要注意:cks本身是伪数组,它身上没有every方法,需要转成真正的数组,才能使用

    完整代码

    1. html>
    2. <html>
    3. <head lang="en">
    4. <meta charset="UTF-8" />
    5. <title>title>
    6. <style>
    7. * {
    8. margin: 0;
    9. padding: 0;
    10. }
    11. table {
    12. border-collapse: collapse;
    13. border-spacing: 0;
    14. border: 1px solid #c0c0c0;
    15. width: 500px;
    16. margin: 100px auto;
    17. text-align: center;
    18. }
    19. th {
    20. background-color: #f40;
    21. font: bold 16px '微软雅黑';
    22. color: #fff;
    23. height: 35px;
    24. }
    25. td {
    26. border: 1px solid #d0d0d0;
    27. color: #404060;
    28. padding: 10px;
    29. }
    30. .allCheck {
    31. width: 80px;
    32. }
    33. style>
    34. head>
    35. <body>
    36. <table>
    37. <thead>
    38. <tr>
    39. <th class="allCheck">
    40. <input type="checkbox" name="" id="checkAll" />
    41. <span class="all">全选span>
    42. th>
    43. <th>商品th>
    44. <th>商家th>
    45. <th>价格th>
    46. tr>
    47. thead>
    48. <tbody class="goods">tbody>
    49. table>
    50. <script>
    51. // 定义商品数据
    52. const goods = [
    53. {
    54. id: +new Date(),
    55. name: '华为手机',
    56. brand: '华为',
    57. price: 5999,
    58. good_status: false,
    59. },
    60. {
    61. id: +new Date(),
    62. name: '小米净水器',
    63. brand: '小米',
    64. price: 2999,
    65. good_status: true,
    66. },
    67. {
    68. id: +new Date(),
    69. name: '三星电视',
    70. brand: '三星',
    71. price: 4999,
    72. good_status: false,
    73. },
    74. ]
    75. // 1 基本渲染
    76. function render() {
    77. let str = ''
    78. goods.forEach(function (item) {
    79. str += `
    80. ${
    81. item.good_status ? 'checked' : ''
    82. }/>
    83. ${item.name}
    84. ${item.brand}
    85. ${item.price}
    86. `
    87. })
    88. document.querySelector('.goods').innerHTML = str
    89. }
    90. render()
    91. // 2 点击全选 让所有商品 跟着变化
    92. // 思路:绑定事件 让所有商品状态与全选按钮状态同步
    93. const checkAll = document.querySelector('#checkAll')
    94. const cks = document.querySelectorAll('.ck') // 伪数组
    95. console.log(Array.isArray(cks)) // false
    96. console.log(cks.forEach) // 本身提供forEach方法用于遍历
    97. // change状态改变时候触发
    98. checkAll.addEventListener('change', function () {
    99. // console.log(this.checked) 全选按钮的状态
    100. const _this = this
    101. cks.forEach(function (item) {
    102. // console.log(this) //window
    103. // console.log(that)
    104. item.checked = _this.checked
    105. })
    106. })
    107. //3 单击每个商品 设置全选按钮状态
    108. // 思路: 单击某个商品 所有商品都选中全选按钮选中,只要有一个未选中全选按钮取消
    109. // cks本身是伪数组,它身上没有every方法,需要转成真正的数组,才能使用
    110. console.log(Array.isArray(Array.from(cks)))
    111. cks.forEach(function (item) {
    112. item.addEventListener('change', function () {
    113. checkAll.checked = Array.from(cks).every(function (item) {
    114. return item.checked
    115. })
    116. })
    117. })
    118. script>
    119. body>
    120. html>

  • 相关阅读:
    LeetCode 362 期周赛
    Linux内核分析(九)--CPU上下文
    2020年12月 C/C++(三级)真题解析#中国电子学会#全国青少年软件编程等级考试
    【网管日记】在服务器docker上安装ubantu可视化桌面
    邱锡鹏神经网络怎么样,邱锡鹏神经网络答案
    Node 之 JavaScript 模块化开发
    神经网络:基本概念、模型与技术
    ES京东搜索
    【编程实践】黑框框里的打字小游戏,但是汇编语言
    Azure Functions Service Bus Trigger 对容器的支持
  • 原文地址:https://blog.csdn.net/m0_56713342/article/details/134533441