Vue实现ToDoList

这是一份由Vue写成的简易ToDoList代码,仅供初学者参考
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ToDoList</title>
    <script src="js/vue.js"></script>
    <style>
        li {
            list-style: none;
        }
        .completecls {
            color: grey;
            text-decoration: line-through;
        }
        .noEdit {
            outline: none;
            border: none;
        }
    </style>
</head>
<body>
    <div id="app">
        <h1>任务列表</h1>
        <p>任务总数:{{tasks.length}},还有{{nocomplete()}}未完成, <input type="button" value="删除" @click="del">已完成任务</p>
        <ul>
            <li v-for="task in tasks">
                <input type="checkbox" v-model="task.isComplete" />
                <input type="text" v-model="task.text" 
                :class="{noEdit:!task.isEdit,completecls:task.isComplete}"
                :disabled="task.isComplete" 
                @focus="task.isEdit=true" 
                @blur="task.isEdit=false" />
            </li>
        </ul>
        <input type="text" placeholder="请输入任务名" v-model="newTask" />
        <input type="button" value="添加" @click="add"/>
    </div>
</body>
</html>
<script>
    let vm = new Vue({
        el: "#app",
        data: {
            newTask: "",
            tasks: [
                {
                    text: "写html",
                    isComplete: false,
                    isEdit: false
                },
                {
                    text: "写css",
                    isComplete: false,
                    isEdit: false
                },
                {
                    text: "写js",
                    isComplete: false,
                    isEdit: false
                },
                {
                    text: "写vue",
                    isComplete: false,
                    isEdit: false
                }
            ],
        },
        methods: {
            add() {
                this.tasks.push({
                    text: this.newTask,
                    isComplete: false,
                });
                this.newTask = "";
            },
            del(){
                this.tasks = this.tasks.filter(item=>item.isComplete==false);
            },
            nocomplete(){
                let count = 0;
                this.tasks.forEach(item=>{
                    if(item.isComplete==false){count++}         
                })
                return count
            }
        }
    })
    
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值