vue 基本语法写的一个小demo

在公众号学习实现的

需要下载一个vue2.0的js,即可

代码如下:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		 <meta name="viewport" content="width=device-width,initial-scale=1.0">
		<title>例子demo</title>
    <style>
          
     .edit{
        display:block ;
        width:80%;
        height: 35px;
        line-height: 35px;
        margin: 30px auto;
        box-sizing: border-box;
        padding-left: 4px;
        border-radius: 4px;
        border:1px solid #ccc;
        outline: 0;
        box-shadow: 0 0 5px #ccc;
     }
       .list{
        margin: 0 auto;
        width:80%;
     }
 .unit{
    position: relative;
    padding: 10px 0;
    border-bottom: 1px solid #efefef;
 }
 .unit:last-child{
    border-bottom: 0;
 }
  .finish{
    text-decoration: line-through;
    color: #ccc;
 }
 .del{
    background: red;
    text-decoration: none;
    position: absolute;
    right:0;
    font-size: 12px;
    border: 0;
    outline: 0;
    padding: 2px 5px;
    border-radius: 5px;
    color: #fff;
 }
  .empty{
    font-size: 13px;
    color: #ccc;
    text-align: center;
    padding: 10px 0;
 }
 
    </style>
	</head>
	<body>
		<div id="app">
        <input 
        class="edit" 
        type="text"
        v-model="task.content"
        placeholder="编写任务"
        name="" id=""
        @keydown.enter = "addTask"
        >
        <div class="list">
            <div class="unit" 
                 v-for="(item,index) in list">
                <input
                  @click="changeState(index)"
                  :checked="item.finished"
                 type="checkbox"
                 >
                <span :class="{'finish':item.finished}">
                  {{item.content}}
                </span>
               <button @click="removeTask(index)" 
                class="del">
                  删除
                </button>
            </div>
            <p v-if="list.length === 0" class="empty">暂无任务</p>
        </div>
    </div>
 
		<script src="js/vue.js"></script>
		<script type="text/javascript">
         const app = new Vue({
            el:"#app",
            data:{
              //默认
           
            task:{
              content:'',  //内容为空
              finished:false,  //未完成
              deleted:false   //未删除
            },

            //任务列表
            list:[]

             },
             methods:{
                //添加任务
                addTask:function(){
                  //将task存入到list数组
                  this.list.push(this.task);
                  //存入list[],后重置task
                   this.task = {
                  content:'',  //内容为空
                  finished:false,  //未完成
                  deleted:false   //未删除
                }
                },
                //循环的任务 是否完成
                changeState:function(index){
                  let curState = this.list[index].finished;
                  this.list[index].finished = !curState;
                },
                //任务的删除  与否
                removeTask:function(index){
                  //使用splice操作删除数组指定项
                  this.list.splice(index,1);
              }
             }
      });


		</script>
	</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值