【问题标题】:How to save click event in local storage如何将点击事件保存在本地存储中
【发布时间】: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();
    });
  });
});

【问题讨论】:

标签: javascript jquery local-storage


【解决方案1】:

在你的代码中用这些交换相关行,它应该可以工作:

//count how many list items there are and display them in the tomato
var tabLength = $('#notificationTab .eagle').length;
if (localStorage.hasOwnProperty('notificationCountViewed') && localStorage.getItem('notificationCountViewed')) {
     $('.notificationCount').text();
} else {
     $('.notificationCount').text(tabLength);
}

还有这个:

//remove notificationCount when bell is clicked
var removeTomato = $('.notificationTrigger').on('click', function() {
  $('.notificationCount').remove();
  localStorage.setItem('notificationCountViewed', true);
});

【讨论】:

    猜你喜欢
    • 2018-02-18
    • 2017-04-27
    • 1970-01-01
    • 2019-04-23
    • 2019-03-30
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多