为什么我的代码不存储localStorage中的项目?休息一切都很好

时间:2017-07-07 12:54:09

标签: javascript html

此代码无法将添加的项目存储到localStorage中。 但它运行良好 当我重新加载页面时,我的所有项目都消失了。 请帮忙。

<!DOCTYPE html>
<html>
<head>
    <title>Todo List</title>

    <!--Let browser know website is optimized for mobile-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <!-- Compiled and minified CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.99.0/css/materialize.min.css">
</head>
<body>
    <div class="container">
        <input id="task"><button class="btn" id="add">Add</button>
        <ul id="todos" class="collection"></ul>
    </div>
    <script type="text/javascript">
       
    </script>
</body>
</html>
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8, 4))
ax1 = fig.add_subplot(121)
ax1.set_xlabel('Credit_History')
ax1.set_ylabel('Count of Applicants')
ax1.set_title("Applicants by Credit_History")
temp1.plot(kind='bar', ax=ax1)

ax2 = fig.add_subplot(122)
ax2.plot(kind='bar')
ax2.set_xlabel('Credit_History')
ax2.set_ylabel('Probability of getting loan')
ax2.set_title("Probability of getting loan by credit history")
temp2.plot(kind='bar', ax=ax2)

1 个答案:

答案 0 :(得分:0)

由于 list 最初为空,并且当您单击 add 按钮时将其保存在 localStorage 中,所有之前添加的项目将被覆盖

localStorage.setItem('todo', JSON.stringify(list)); // This is overriding your items from the last session

如果要保留上一个会话中的项目,可以使用 localStorage 而不是空数组初始化列表

list = JSON.parse(localStorage.getItem('todo')); // This will persist your items from the last session