首页 > 其他分享 >jquery 待办事项

jquery 待办事项

时间:2023-05-30 16:34:43浏览次数:31  
标签:jquery function console log index 待办 var 事项 data


jquery 待办事项_数据


index.html

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>ToDoList—最简单的待办事项列表</title>
    <link rel="stylesheet" href="css/index.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/todolist.js"></script>
</head>

<body>
    <header>
        <section>
            <label for="title">待办事项</label>
            <input type="text" id="title" name="title" placeholder="添加待办事项" required="required" autocomplete="off" />
        </section>
    </header>
    <section>
        <h2>正在进行 <span id="todocount"></span></h2>
        <ol id="todolist" class="demo-box">

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

        </ul>
    </section>
    <footer>
        Copyright © 2014 todolist.cn
    </footer>


</body>

</html>

todolist.js

$(function () {
    load();
    $('#title').on('keydown', function (event) {
        if (event.keyCode == 13) {
            // 先读取本地存储原来的数据
            var local = getData();
            console.log(local)
            // 把local数组更新
            if ($(this).val())
                local.push({title: $(this).val(), done: false})
            // 把数组存储到本地存储
            saveData(local);
            $(this).val('')
            load();
        }
    })

    // todoList 删除操作
    $('ol, ul').on('click', 'li a', function () {
        // console.log($(this).parent().index());
        var data = getData();
        data.splice($(this).parents('li').attr('data-index'), 1)    //从下标开始删除1个元素
        saveData(data);
        load();
        // $(this).parent().remove();
    })
    // checked
    $('ol, ul').on('click', 'input', function () {
        // console.log(1)
        var data = getData();
        var index = $(this).parents('li').attr('data-index');
        // console.log(index)
        data[index].done = $(this).prop('checked');
        saveData(data);
        // console.log(data)
        load();
    })

    // 读取本地存储的数据
    function getData() {
        var data = localStorage.getItem('todoList');
        if (data) {
            return JSON.parse(data)
        }
        return [];
    }

    // 保存本地存储数据
    function saveData(data) {
        localStorage.setItem('todoList', JSON.stringify(data))
    }

    // 渲染加载数据
    function load() {
        var finished = 0;
        var isDoing = 0;
        var data = getData();
        // 清空之前的数据.
        $('ol').empty();
        $('ul').empty();
        $.each(data, function (i, k) {
            // console.log(k);
            if (k.done)
                finished++;
            else
                isDoing++;
            if (k.done)
                $('ul').prepend($('<li data-index=' + i + '><input type="checkbox" checked="checked"> <p> ' +
                    '' + k.title + '</p>  ' + '<a href="javascript:;"></a></li>'))
            else
                $('ol').prepend($('<li data-index=' + i + '><input type="checkbox"> <p> ' + k.title + '</p>  ' + '<a href="javascript:;"></a></li>'))
        })
        $('#todocount').text(isDoing);
        $('#donecount').text(finished);
    }
})


标签:jquery,function,console,log,index,待办,var,事项,data
From: https://blog.51cto.com/u_16144724/6380364

相关文章

  • jquery本地存储的数据格式只能是字符串,如需存储对象,需要转换后存储
    <!DOCTYPEhtml><htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="js/jquery-3.5.1.min.js"></script> </head> <body> <scri......
  • jquery 瀑布流
    pinterest_grid.js<!DOCTYPEhtml><htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <linkrel="stylesheet"href="css/normalize.css"> <linkrel=&......
  • jquery 拷贝对象
    如果想要把某个对象拷贝(合并)给另外一个对象使用,此时可以使用$.extend()方法语法∶$.extend([deep],target,object1,[object])1.deep:如果设为true为深拷贝,默认为false浅拷贝2.target:要拷贝的目标对象3.object1:待拷贝到第一个对象的对象。案例:<!DOCTYPEhtml><htmllang=......
  • jquery自动触发事件
    <!DOCTYPEhtml><htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="js/jquery-3.5.1.min.js"></script> <script>$(function......
  • jQuery 事件解绑
    事件.off([事件名][,[子元素名]])一次性事件:事件.one(),//用法同on<!DOCTYPEhtml><htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="js/jquery-3.5.1.min.js"></sc......
  • jquery 创建,追加,删除节点
    <!DOCTYPEhtml><htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="js/jquery-3.5.1.min.js"></script> </head> <body> <ul&g......
  • jquery 全选,反选复选框
    <!DOCTYPEhtml><htmllang="zh-CN"> <head> <metacharset="UTF-8"> <title>我的购物车-品优购</title> <linkrel="stylesheet"href="css/base.css"> <linkrel="styleshee......
  • odoo 邮箱注意事项
    outserver配置gmail,1,开启2步认证2:设置应用密码, 使用应用密码配置out mailserver     配置QQ邮箱, 需要开题stmp服务,使用专用的stmp密码设置i一次,而不是邮箱本省的密码  错误: SMTPSenderRefused:501mailfromaddressmustbesameas......
  • 泛型的注意事项:擦除问题、基本数据类型
        ......
  • ElementUI的form表单验证注意事项
    ElementUI的form表单验证注意事项1.踩过的坑,记录一下。验证表单时一直提示必填项未填写,实际已经填写了。2.el-form的正确使用流程el-form就是最外层的form表单,做验证有三个必填属性,不填写验证就会不正确。ref属性:相当于ID,稍后的提交按钮函数会用到它。:model:绑定要用......