vue小练习之全反选案例

html 复制代码
<!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>Document</title>
</head>

<body>
  <div id="app">
    <div>
      <span>全选:</span>
      <input type="checkbox" v-model="isAll" />
      <button @click="reverseBtn">反选</button>
      <ul>
        <li v-for="(item,index) in arr" :key="index">
          <input type="checkbox"  v-model="item.checked"/>
          <span>{{item.name}}</span>
        </li>
      </ul>
    </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        arr: [
          {
            name: "猪八戒",
            checked: false,
          },
          {
            name: "孙悟空",
            checked: false,
          },
          {
            name: "唐僧",
            checked: false,
          },
          {
            name: "白龙马",
            checked: false,
          },
        ],
      },
      computed:{
        isAll:{
            get(){
                return this.arr.every(item=>item.checked)
            },
            set(value){
                this.arr.forEach(item=>item.checked=value)
            }

        }

      },
      methods:{
        reverseBtn(){
            this.isAll=!this.isAll
        }
      }
    })
  </script>


</body>

</html>
相关推荐
zhangxingchao3 小时前
Kotlin常用的Flow 操作符整理
前端
IT_陈寒5 小时前
React的useState居然还有这种坑?我差点删库跑路
前端·人工智能·后端
Pedantic6 小时前
SwiftUI 手势笔记
前端·后端
橙子家6 小时前
浏览器缓存之【结构化数据库与缓存】: IndexedDB、Cache storage 和 Storage buckets
前端
user20585561518136 小时前
X6 中边悬浮置顶,规避 `mouseleave` 事件丢失问题
前端
李明卫杭州6 小时前
CSS aspect-ratio 属性完全指南
前端
Pedantic8 小时前
SwiftUI 手势层级(Gesture Hierarchy)详解
前端
飘尘9 小时前
前端转型全栈(Java后端)的快速上手指引
前端·后端·全栈
一颗烂土豆9 小时前
Meshopt 压缩深度解析,为什么它比 Draco 更快
前端·javascript·webgl