这是一份由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>