javascript - 使用 HTML5 本地存储来存储列表项 <ul>

标签 javascript jquery css html

我正在创建一个基本的待办事项列表,想知道如何存储我的列表,以便当用户返回页面或不小心刷新浏览器窗口时,该列表仍然可用?

html

<!DOCTYPE html>
<html>

    <head>
        <title>My To-Do List</title>
        <link rel="stylesheet" href="css/styles.css" />
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="css/font-awesome-animation.min.css">
        <link href='https://fonts.googleapis.com/css?family=Oswald:400,300,700' rel='stylesheet' type='text/css'>
        <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
        <link rel="icon" href="/favicon.ico" type="image/x-icon">
    </head>

    <body>
        <div id="page">
            <header>
                <img src="images/checklist.png" alt="some_text">
            </header>
             <h2>MY TO-DO LIST</h2>

            <ul id="sortable"></ul>
            <form id="newItemForm">
                <input type="text" id="itemDescription" placeholder="Add Description" maxlength="40" />
                <input type="submit" id="add" value="add" />
                <div id="double">Drag and drop to rearrange items
                    <br />Click on an item to remove it</div>
            </form>
        </div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <script src="js/main.js"></script>
        <script src="js/sort.js"></script>
        <script src="jquery-ui/jquery-ui.js"></script>
    </body>

</html>

JavaScript/jQuery

$(function () {

    var $list;
    var $newItemForm;
    var $newItemButton;
    var item = '';
    $list = $('ul');
    $newItemForm = $('#newItemForm');
    $newItemButton = $('#newItemButton');

    // ADDING A NEW LIST ITEM
    $newItemForm.on('submit', function (e) {
        e.preventDefault();
        var text = $('input:text').val();
        $list.append('<li>' + text + '</li>');
        $('input:text').val('');
    });

    $list.on('click', 'li', function () {
        var $this = $(this);
        var complete = $this.hasClass('complete');

        if (complete === true) {
            $this.animate({}, 500, 'swing', function () {
                $this.remove();
            });
        } else {
            item = $this.text();
            $this.remove();
        }
    });

});

localStorage.setItem($list);

//add animations when you learn how to...

最佳答案

您还需要将数据保存在一个对象中。目前它仅在 DOM 中。您添加新待办事项或编辑现有待办事项的所有内容,您都需要将其保存到本地存储。将 DOM 节点存储到 localStorage 是行不通的。 localStorage 也只接受字符串值。

这就是我将如何更改您的代码:

// localStorage key
var lsKey = 'TODO_LIST';

// keeping data
var todoList = {};

function getSavedData () {
    var fromLs = localstorage.getItem( lsKey );

    if ( !! fromLs ) {
        todoList = JSON.parse( fromLs );
    } else {
        todoList = {};
        localstorage.setItem( lsKey, todoList );
    };
};

function saveData () {
    var stringify = JSON.stringify( todoList );
    localstorage.setItem( lsKey, todoList );
};

$newItemForm.on('submit', function(e) {
    e.preventDefault();

    var text = $('input:text').val().trim(),
        uuid = new Date.now();

    // lets use input[type:checkbox] to determine if complete or not
    if ( !! text ) {
        todoList[uuid] = text;
        $list.append('<li><input type="checkbox" id=' + uuid + ' /> ' + text + '</li>');
        $( 'input:text' ).val( '' );
    };
};

$list.on('change', 'li input', function() {
    var uuid = $(this).attr( 'id' ),
        $li  = $(this).parent();

    if ( $(this).prop('checked') ) {
        todoList[uuid] = undefined;
        delete todoList[uuid];

        saveData();

        $li.fadeOut("slow", function() {
            $this.remove();
        };
    };
});

祝你好运,玩得开心!

关于javascript - 使用 HTML5 本地存储来存储列表项 <ul>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33773566/

相关文章:

javascript - 单击按钮时面包屑菜单会更新吗?

javascript - Arduino 上的以太网 Web 服务器,具有动态数据

javascript - 如何在 JQuery 中的特定事件上再次启用开发工具

javascript - 作为 jQuery 选择器的字符串数组?

javascript - jQuery innerwidth/innerheight 无法在 Chrome/Safari 中正确处理图像

html - 在 lg 上只有一个 div 作为 xs 分辨率

javascript - socket.io 中的 SetTimeout 第一次不起作用

javascript - ASP.NET MVC RedirectToAction 在 View 中的 AJAX 发布后不起作用

html - 仅使用 CSS 将 div 高度设置为浏览器的高度

javascript - 移动版 Safari 浏览器的滚动问题