• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Vue实现简易记事本功能

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文实例为大家分享了Vue实现简易记事本功能的具体代码,供大家参考,具体内容如下

预览图:

功能如下:

(1)输入任务并按下回车键,可将任务添加至任务列表(不可输入重复任务)

(2)点击删除,可删除对应任务

(3)点击清空,所有任务都会被删除

(4)左下角同步显示任务总数

完整代码如下:

<!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>记事本</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
 
        #todoapp {
            width: 600px;
            background-color: rgba(19, 161, 114, 0.63);
            font-family: sans-serif;
        }
 
        .header>h1 {
            padding: 20px 0;
            text-align: center;
            font-size: 40px;
            color: whitesmoke;
        }
 
 
        .newTask {
            display: block;
            width: 500px;
            height: 50px;
            line-height: 50px;
            padding-left: 10px;
            margin: 0 auto;
            font-size: 20px;
            outline: none;
            border: none;
        }
 
        .todolist li {
            height: 30px;
            line-height: 30px;
            padding-left: 15px;
            margin: 10px 0;
            font-size: 25px;
            color: white;
        }
 
        .todolist .item {
            margin-left: 15px;
        }
 
        .destroy,
        .clear {
            width: 50px;
            height: 30px;
            float: right;
            color: white;
            background-color: transparent;
            border: none;
            font-size: 20px;
        }
 
        .footer {
            width: 600px;
            height: 30px;
            padding: 10px 0;
            vertical-align: middle;
        }
 
 
        .footer p {
            display: inline-block;
            padding-left: 15px;
            color: white;
            font-size: 20px;
        }
    </style>
</head>
 
<body>
    <section id="todoapp">
        <header class="header">
            <h1>记事本</h1>
            <input type="text" v-model="newItem" class="newTask" placeholder="请输入任务" @keyup.enter="add">
        </header>
        <section>
            <ul class="todolist">
                <li v-for="(item, index) in list">
                    <div>
                        <span>{{ index + 1 }}</span>
                        <label class="item">{{ item }}</label>
                        <button class="destroy" @click="del(index)">删除</button>
                    </div>
                </li>
            </ul>
        </section>
        <footer class="footer">
            <p class="count">
                items: {{ list.length }}
            </p>
            <button class="clear" @click="clear" v-show="list.length != 0">清空</button>
        </footer>
    </section>
    <script src="./vue.js"></script>
    <script>
        const app = new Vue({
            el: "#todoapp",
            data: {
                list: [],
                newItem: ""
            },
            methods: {
                add() {
                    if (this.newItem == "") {
                        return;
                    }
                    else {
                        if (!this.list.includes(this.newItem)) {
                            this.list.push(this.newItem);
                            this.newItem = "";
                        }
                        else {
                            alert("请勿添加重复事件!");
                            this.newItem = "";
                        }
                    }
                },
                del(index) {
                    this.list.splice(index, 1);
                },
                clear() {
                    this.list = [];
                }
            }
        })
    </script>
</body>
 
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持极客世界。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
vue全局引入scss(mixin)发布时间:2022-02-05
下一篇:
JavaScript闭包详解发布时间:2022-02-05
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap