【问题标题】:Toggle classes one click using jQuery and save last state使用 jQuery 一键切换类并保存上一个状态
【发布时间】:2018-08-11 22:27:28
【问题描述】:

所以我试图结合在 stackoverflow 上发布的三个问题的答案; Save current state after jquery click functionSave data to local storageHTML5 local storage save .toggleClass。我想要做的是使用来自 jQuery 的 .one() 在两个类 onclick 之间切换。然后在切换类之后。

我想保存最后添加的课程。到目前为止,我的代码如下:

jQuery

$(".post-tag").one('click', function() {
  $(this).find('i').toggleClass('fa-meh fa-smile-beam text-warning text-success');

  var likeState = $(this).find('i').hasClass('fa-smile-beam');
  localStorage.setItem('liked', likeState);

  var likedStorage = localStorage.getItem('liked');

  if(likedStorage && $(this).find('i').hasClass('fa-smile-beam')) {
    $(this).find('i').removeClass('fa-meh')
  }
});

HTML

<div class="post-tag-wrapper">
    <div class="post-tag shadow">
        <i class="far fa-meh text-warning"></i>
    </div>
</div>

我怎样才能做到这一点?

注意:我知道如果我将类添加到Field Object;例如,如果使用MongoDB;默认为fa-mehonclick 使用一些AJAX 会将Object 中的Field 更新为 fa-smile-beam。但需要使用LocalStorage

注意: 根据MDN,LocalStorage 违反了政策决定,所以 这不是节省用户交互的做法。

【问题讨论】:

  • 我想了解您为什么要尝试将类状态保存到本地存储中?这是什么提议?跨页面加载保持状态?或者你想要完成什么?可能有更好的方法。
  • @Amir 我正在尝试为初始状态为fa-meh 的帖子创建一个like button,当单击该类更改为fa-smile-beam 时,当我刷新页面时,该类是仍然是fa-smile-beam 不是fa-meh。这是一条有用的评论吗?
  • @Amir 我知道如果我将类添加到Field 中的Object 中是可行的;例如,如果使用 MongoDB;默认为fa-mehonclick 使用一些AJAX 会将Object 中的Field 更新为fa-smile-beam。但我需要使用LocalStorage 来完成它。

标签: javascript jquery bootstrap-4 local-storage session-storage


【解决方案1】:

好吧,你可以做这样的事情。您的起点的主要问题是您不能在同一个功能中执行它们。您将需要两个单独的功能。

  1. 来回切换状态。我建议使用 .on 绑定方法而不是 .one,除非您不希望用户能够撤消他们的操作。

  2. 当文档加载时,除非您有其他方法来设置按钮的正确状态,否则您必须设置状态,即保存在按钮的本地存储中。

HTML

<button class="post-tag" id="some-unique-id">
  <i class="fa-meh"></i> Like
</button>

JavaScript

// This will make sure that the state is toggled on user click
// The user can click back and forth, and the state will be saved each time
$(".post-tag").on('click', e => {
  const $elm = $(e.currentTarget);
  // If you have more than one button you'll need unique IDs for them to make this work
  const uniqueId = $elm.attr('id');
  const $i = $elm.find('i');

  $i.toggleClass('fa-meh fa-smile-beam text-warning text-success');

  const likeState = $i.hasClass('fa-smile-beam');
  localStorage.setItem(`likeState-${id}`, likeState);
});

// To persist the change you'll have to run this on page load as well
$( document ).ready(function() {
  const $postTags = $('.post-tag');
  $postTags.each((elm) => {
    const $elm = $(elm);
    const uniqueId = $elm.attr('id');
    const $i = $elm.find('i');

    // Now you apply the state stored in local sotrage to your buttons.
    const likedStorage = localStorage.getItem(`likeState-${id}`);
    if(likedStorage && $i.hasClass('fa-smile-beam')) {
      $i.removeClass('fa-meh')
    }
  });
});

【讨论】:

  • 你的回答很有道理。但由于某种原因,它不起作用。我不知道为什么!!!
  • @Tes3awy 你能创建一个你所拥有的 jsfiddle 以便我可以进一步帮助你吗?
猜你喜欢
  • 2014-05-22
  • 1970-01-01
  • 2013-08-16
  • 2019-12-28
  • 2019-02-11
  • 1970-01-01
  • 1970-01-01
  • 2021-06-13
  • 2014-08-13
相关资源
最近更新 更多