【发布时间】:2019-05-23 00:27:48
【问题描述】:
我想创建一个通知铃小部件,它可以拉出所有带有标签theBell 的文章。
当用户单击打开包含此文章的列表的图标时,通知计数和单击事件应保存在本地存储中,以便当用户刷新页面时浏览器知道这些文章已“被阅读”(在我的情况下,该图标已被单击)。
我的问题是如何捕获该点击事件并将其保存到本地存储?
$.each(data.articles, function(index, item) {
var style1 = '<li class = "eagle"><a href="' + item.html_url + '">' + item.title + '</a><span class = "eagleClose">x</span></li>'
$('#notificationTab').append(style1);
$('#notificationTab').each(function() {
var str = document.getElementById("notificationTab").innerHTML;
var res = str.replace(/null/g, ' ');
document.getElementById("notificationTab").innerHTML = res;
//count how many list items there are and display them in the tomato
var tabLength = $('#notificationTab .eagle').length;
$('.notificationCount').text(tabLength);
//remove notificationCount when bell is clicked
var removeTomato = $('.notificationTrigger').on('click', function() {
$('.notificationCount').remove();
});
});
});
【问题讨论】:
-
@evolutionxbox 为什么? localStorage 很好。
-
这里是 MDN 指南,它展示了如何保存和读取 localStorage 数据:developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
-
比本地存储更“永久”?
-
确实如此,但似乎与所询问的内容无关。
标签: javascript jquery local-storage