jq实现todolist

在这里插入图片描述
html

<!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">
    <link rel="stylesheet" href="css/index.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/index.js"></script>
    <title>Document</title>
</head>

<body>
    <div class="head">
        <div class="headtop w">
            <div class="title">ToDoList</div>
            <input type="text" id="text" placeholder="添加ToDo">
        </div>
    </div>
    <div class="content">
        <div class="w">
            <h2>正在进行<span id="todocount"></span></h2>
            <ol id="todolist" class="demo-box">

            </ol>
            <h2>已经完成<span id="donecount"></span></h2>
            <ul id="donelist">

            </ul>
        </div>
    </div>
    <footer class="w footer">Copyright @ 2014 todolist.cn</footer>
</body>

</html>

scss

.w {
    width: 600px;
    margin: 0 auto;
}

* {
    margin: 0;
    padding: 0;
    list-style: none;
}

body {
    background-color: rgb(211, 210, 210);
}

input {
    outline: none;
}

a {
    text-decoration: none;
}

.head {
    background-color: black;
    .headtop {
        height: 60px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .title {
            color: #fff;
            font-size: 20px;
        }
        #text {
            width: 350px;
            height: 30px;
            border-radius: 3px;
            box-shadow: 1px 1px 1px #888;
        }
    }
}

.content {
    .w {
        h2 {
            margin-top: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            span {
                width: 20px;
                height: 20px;
                font-size: 16px;
                text-align: center;
                line-height: 20px;
                background-color: #fff;
                border-radius: 50%;
            }
        }
        #todolist {
            width: 600px;
            display: flex;
            flex-wrap: wrap;
            justify-content: flex-end;
            // background-color: skyblue;
            li {
                margin: 5px 0;
                display: flex;
                background-color: #fff;
                align-items: center;
                width: 600px;
                height: 40px;
                position: relative;
                div {
                    width: 8px;
                    height: 40px;
                    background-color: skyblue;
                }
                input {
                    margin-left: 5px;
                    width: 20px;
                    height: 20px;
                }
                p {
                    padding-left: 5px;
                }
                a {
                    position: absolute;
                    right: 0;
                    width: 18px;
                    height: 18px;
                    border-radius: 50%;
                    border: 5px solid skyblue;
                    background-color: #999;
                    margin-right: 5px;
                }
            }
        }
        #donelist {
            width: 600px;
            display: flex;
            justify-content: flex-end;
            flex-wrap: wrap;
            li {
                display: flex;
                flex-wrap: wrap;
                background-color: rgba(255, 255, 255, 0.381);
                align-items: center;
                width: 590px;
                height: 40px;
                position: relative;
                margin: 5px auto;
                div {
                    width: 8px;
                    height: 40px;
                    background-color: skyblue;
                }
                input {
                    margin-left: 5px;
                    width: 20px;
                    height: 20px;
                }
                p {
                    padding-left: 5px;
                }
                a {
                    position: absolute;
                    right: 0;
                    width: 18px;
                    height: 18px;
                    border-radius: 50%;
                    border: 5px solid #fff;
                    background-color: #999;
                    margin-right: 5px;
                }
            }
        }
    }
}

.footer {
    margin-top: 20px;
    display: flex;
    justify-content: center;
    color: #999;
}

js

//回车13
$(function() {
    load();
    $('.head').on('keydown', function(e) {
        if (e.keyCode === 13) {
            if ($('#text').val() === '') {
                alert('请输入要记的内容');
            } else {
                //先读取本地存储原来的数据
                var local = getDate();
                //把local数组进行更新数据 把最新的数据 追加给local数组
                local.push({ title: $('#text').val(), done: false });
                //把这个数据存储给本地存储
                saveDate(local);
                load();
                $('#text').val('');
            }
        }
    });
    //todolist删除操作
    $('ol,ul').on('click', 'a', function() {
        //先获取本地存储
        var data = getDate();
        //修改数据
        var index = $(this).attr('id');
        data.splice(index, 1); //数组删除方式  第一个参数是从哪儿删  第二个参数是删几个
        //保存到本地
        saveDate(data);
        //重新渲染页面
        load();


    });
    //正在进行
    $('ol,ul').on('click', 'input', function() {
        //先获取本地存储数据
        var data = getDate();
        var index = $(this).siblings('a').attr('id');
        //修改数据
        data[index].done = $(this).prop('checked');
        //保存到本地
        saveDate(data);
        //渲染页面
        load();


    });
    //读取本地数据的函数
    function getDate() {
        var data = localStorage.getItem('todolist');
        if (data !== null) {
            //本地存储里面的数据是字符串格式  我们需要对象格式
            return JSON.parse(data);
        } else {
            return [];
        }
    }
    //保存本地存储数据
    function saveDate(data) {
        localStorage.setItem('todolist', JSON.stringify(data));
    }
    //渲染加载数据
    function load() {
        var data = getDate();
        var todocount = 0; //正在进行的个数
        var donecount = 0; //已经完成的个数
        console.log(data);
        //遍历之前先要清空里面的元素内容
        $("ol,ul").empty();
        //遍历数据
        $.each(data, function(i, n) {
            if (n.done) {
                donecount++;
                $('ul').prepend("<li><div></div></div><input type='checkbox' checked='checked'><p>" + n.title + "</p><a href='javascript:;' id=" + i + "></a> </li>");

            } else {
                todocount++;
                $('ol').prepend("<li><div></div></div><input type='checkbox'><p>" + n.title + "</p><a href='javascript:;' id=" + i + "></a> </li>");
            }

        });
        $('#todocount').text(todocount);
        $('#donecount').text(donecount);

    }

})

还需要jquery.min.js 去下载即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的todolist示例,使用Vuex实现状态管理: 1. 安装vuex ``` npm install vuex --save ``` 2. 创建store.js文件,并在其中定义state、mutations、actions和getters ```javascript import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const state = { todoList: [] } const mutations = { addTodo (state, todo) { state.todoList.push(todo) }, deleteTodo (state, todo) { const index = state.todoList.indexOf(todo) state.todoList.splice(index, 1) }, updateTodo (state, { todo, text }) { todo.text = text }, completeTodo (state, todo) { todo.completed = !todo.completed }, clearCompletedTodos (state) { state.todoList = state.todoList.filter(todo => !todo.completed) } } const actions = { addTodo ({ commit }, text) { commit('addTodo', { text: text, completed: false }) }, deleteTodo ({ commit }, todo) { commit('deleteTodo', todo) }, updateTodo ({ commit }, { todo, text }) { commit('updateTodo', { todo, text }) }, completeTodo ({ commit }, todo) { commit('completeTodo', todo) }, clearCompletedTodos ({ commit }) { commit('clearCompletedTodos') } } const getters = { completedTodos: state => state.todoList.filter(todo => todo.completed), activeTodos: state => state.todoList.filter(todo => !todo.completed) } export default new Vuex.Store({ state, mutations, actions, getters }) ``` 3. 在main.js中引入store.js,并将其注入到Vue实例中 ```javascript import Vue from 'vue' import App from './App.vue' import store from './store.js' new Vue({ el: '#app', store, render: h => h(App) }) ``` 4. 在组件中使用store ```javascript <template> <div> <input v-model="newTodoText" @keyup.enter="addNewTodo"> <ul> <li v-for="todo in todos" :key="todo.id"> <input type="checkbox" v-model="todo.completed" @change="completeTodo(todo)"> <input type="text" v-model="todo.text" @blur="updateTodoText(todo)" :disabled="todo.completed"> <button @click="deleteTodo(todo)">Delete</button> </li> </ul> <button @click="clearCompletedTodos">Clear Completed</button> </div> </template> <script> export default { computed: { todos () { return this.$store.state.todoList } }, methods: { addNewTodo () { if (!this.newTodoText) return this.$store.dispatch('addTodo', this.newTodoText) this.newTodoText = '' }, deleteTodo (todo) { this.$store.dispatch('deleteTodo', todo) }, updateTodoText (todo) { this.$store.dispatch('updateTodo', { todo, text: todo.text }) }, completeTodo (todo) { this.$store.dispatch('completeTodo', todo) }, clearCompletedTodos () { this.$store.dispatch('clearCompletedTodos') } }, data () { return { newTodoText: '' } } } </script> ``` 以上就是一个简单的使用Vuex实现todolist的示例。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值