How to differentiate localStorage from storing each file on click?

前端 未结 2 1816
南旧
南旧 2020-12-21 01:21

I\'ve made a simple jQuery script which stores values \"voted1\", \"voted2\", \"voted3\" in localStorage. The problem is

2条回答
  •  别那么骄傲
    2020-12-21 02:02

      $(document).ready(function() {
        var voteLink = $(".gallery-item a");
        var votedYes = $(".exists");
        if (localStorage.getItem("count") === null) {
          localStorage.setItem("count", 1)
        }
        if (!(localStorage.getItem("voted3") === "yes3")) {
          var i = Number(localStorage.getItem("count")),
            fn = function(e) {
              if (i < 3) {
                localStorage.setItem("voted" + i, "yes" + i);
                $(this).text("Thank you! for vote " + i)
                  .addClass("voted" + i);
                localStorage.setItem("count", 1 + i);
                i = Number(localStorage.getItem("count"));
              } else {
                localStorage.setItem("voted" + i, "yes" + i);
                $(this).text("Thank you! for vote " + i)
                  .addClass("voted" + i)
                  .fadeOut("slow");
                if (localStorage.getItem("voted3") === "yes3") {
                  voteLink.remove();
                  votedYes.fadeIn(1800);
                }
              }
            };
          voteLink.on("click", fn);
        } else {
          // if `localStorage` has property `"voted3"` and value equals `"yes3"`, 
          // do stuff
        }
      })
    

提交回复
热议问题