美文网首页
vue-todolist

vue-todolist

作者: 撕心裂肺1232 | 来源:发表于2019-03-17 18:22 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>v-for练习</title>
            <style>
                p{
                    display: inline;
                }
            </style>
        </head>
        <body>
            <div id="app">
                <div>
                    <label>时间:</label><input type="text" placeholder="时间" v-model="newList.time">
                </div>
                <div>
                    <label>事件:</label><input type="text" placeholder="事件" v-model="newList.thing">
                </div>
                <button @click='addNewList'>添加计划</button>
                
    
               <div>今日计划:</div>
               <div v-for="(list, key) in lists">
                    <p>{{list.time}}</p>
                    <p>{{list.thing}}</p>
                    <button @click= 'deleteList(key)'>删除</button>
               </div>
            </div>
            <script src="js/vue.min.js"></script>
            <script>
                let vm = new Vue({
                    el: '#app',       
                    data: {            
                        lists: [
                            {time: '8点', thing: '起床'}
                        ],
                        newList: { time: '', thing: ''}
                    },
                    methods: {
                        addNewList: function() {
                            if(this.newList.time === ''){
                                alert('请输入时间');
                                return
                            }
    
                            if(this.newList.thing === ''){
                                alert('请输入事件');
                                return
                            }
    
                            this.lists.unshift(this.newList);
                            this.newList = { time: '', thing: ''};
                        },
                        deleteList: function(key) {
                            console.log(key);
                            this.lists.splice(key, 1);
                        }
                    }
                })
            
            </script>
        </body>
    </html>
    
    todolist.PNG

    相关文章

      网友评论

          本文标题:vue-todolist

          本文链接:https://www.haomeiwen.com/subject/wjgumqtx.html