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>
相关推荐
码上成长1 天前
GraphQL:让前端自己决定要什么数据
前端·后端·graphql
冴羽1 天前
为什么在 JavaScript 中 NaN !== NaN?背后藏着 40 年的技术故事
前端·javascript·node.js
久爱@勿忘1 天前
vue下载项目内静态文件
前端·javascript·vue.js
前端炒粉1 天前
21.搜索二维矩阵 II
前端·javascript·算法·矩阵
合作小小程序员小小店1 天前
web网页开发,在线%台球俱乐部管理%系统,基于Idea,html,css,jQuery,jsp,java,ssm,mysql。
java·前端·jdk·intellij-idea·jquery·web
不爱吃糖的程序媛1 天前
Electron 应用中的系统检测方案对比
前端·javascript·electron
泷羽Sec-静安1 天前
Less-9 GET-Blind-Time based-Single Quotes
服务器·前端·数据库·sql·web安全·less
pe7er1 天前
用高阶函数实现递归:从匿名函数到通用递归生成器
前端·javascript
IT古董1 天前
全面理解 Corepack:Node.js 的包管理新时代
前端·node.js·corepack
Jonathan Star1 天前
NestJS 是基于 Node.js 的渐进式后端框架,核心特点包括 **依赖注入、模块化架构、装饰器驱动、TypeScript 优先、与主流工具集成** 等
开发语言·javascript·node.js