javascript - 将项目添加到收藏夹并将其保存在 Cookie 中
问题描述
我有这个项目:
<li><a style="display: none">Movie</a><div id="movie-item" class="filterDiv thriller science-fiction action horror war 2018"></div></li>
还有一个按钮,可让您将该项目保存在收藏夹中:
<button onclick="document.getElementById('movie-item').item.classList.add('favorites');">Mark as favorite</button>
这很完美,因为当我点击“收藏夹”部分时,该项目会正确显示!但问题是:我想创建一个 Cookie 来保存该项目被标记为收藏。
示例:https ://codepen.io/anon/pen/GzOmNe#anon-login
有人可以帮助我吗?谢谢
解决方案
我不建议使用 cookie。您应该改用 localStorage。
function saveFav () {
document.getElementById('movie-item').classList.add('favorites');
// Save
localStorage.setItem('favorites', 'movie');
// Get
var fav = localStorage.getItem('favorites');
console.log(fav);
}
<li><a style="display: none">Movie</a><div id="movie-item" class="filterDiv thriller science-fiction action horror war 2018"></div></li>
<button onclick="saveFav()">Mark as favorite</button>
推荐阅读
- polymer - 聚合物 3.x 中聚合物 1.x 的“行为”对应物是什么
- hyperledger-fabric - 如何在节点链码中使用 stub.getTransient()
- python - 如何在python中配对两个元组?
- charts - 如何在谷歌饼图下写文字
- flutter - Flutter中垂直展开ListView
- .net-core - 如何不对我的通用主机使用的实体框架核心数据库的设置进行硬核?
- c++ - gcc 允许未初始化的 constexpr
- c# - 无法使用 ASPOSE Slides 设置表格单元格的边距
- java - 如何修复 ORACLE-ADF 中 FacesContext 的空指针异常
- java - 我的带有 FileWriter 的 Java 代码不起作用