如何使用JavaScript存储新创建的元素或cookie?

发布于 2025-02-04 09:41:30 字数 1479 浏览 5 评论 0原文

我正在制作一个闲置的点击器游戏,以娱乐,一切都很好,直到遇到问题。

我基本上想发生的事情是,当单击图像并且点击元素元素超过一个时,创建了新的图像元素。这里没问题,主要问题是保存图像。如果用户刷新页面,我希望创建的元素仍然存在。我尝试使用 OuterHtml ,并遵循其他一些堆栈溢出论坛问题,但我永远无法找到适当的解决方案。我还尝试了 localstorage cookie ,但我相信我使用它们错误。

我的代码在下面,我的SoloLearn将在下面链接,其中包括我项目的完整代码。

 function oneHundThou() {

   var countvar = document.getElementById("clickCounter")
    if(document.getElementById("clickCounter").innerHTML > 1) {
      alert("Achievement! 1 pat!")
      
      var achievement1k = document.createElement("img");
     

      // create a cookie so when the user refreshes the page, the achievement is shown again 

      document.cookie = "achievement1k=true";
      achievement1k.src = "https://c.pxhere.com/images/f2/ec/a3fcfba7993c96fe881024fe21e7-1460589.jpg!d";
      
      achievement1k.style.height = "1000px"
      achievement1k.style.width = "1000px"
      achievement1k.style.backgroundColor = "red"
      
      document.body.appendChild(achievement1k);
      oneHundThou = function(){}; // emptying my function after it is run once instead of using a standard switch statement
    }
    else {
      return
    }

  }
  oneHundThou();
  

我知道还有另一个与此相似的帖子,但是,我的答案无法在该帖子上回答。

完整代码在这里: https://code.sololearn.com/wwwdw59oenfqb > 请帮忙!谢谢。 (:

I am making an idle clicker game for fun, everything was going fine until I encountered a problem.

What I basically want to happen is when the image is clicked and the clickCounter element is over one, the new image element is created. No problem here, the main problem is saving the image. If the user refreshes the page, I want the created element to still be there. I have tried using outerHTML and following some other Stack Overflow forum questions but I could never get a proper solution to this certain problem. I have also tried localStorage and cookies but I believe I am using them wrong.

My code is below, my sololearn will be linked below, consisting of the full code to my project.

 function oneHundThou() {

   var countvar = document.getElementById("clickCounter")
    if(document.getElementById("clickCounter").innerHTML > 1) {
      alert("Achievement! 1 pat!")
      
      var achievement1k = document.createElement("img");
     

      // create a cookie so when the user refreshes the page, the achievement is shown again 

      document.cookie = "achievement1k=true";
      achievement1k.src = "https://c.pxhere.com/images/f2/ec/a3fcfba7993c96fe881024fe21e7-1460589.jpg!d";
      
      achievement1k.style.height = "1000px"
      achievement1k.style.width = "1000px"
      achievement1k.style.backgroundColor = "red"
      
      document.body.appendChild(achievement1k);
      oneHundThou = function(){}; // emptying my function after it is run once instead of using a standard switch statement
    }
    else {
      return
    }

  }
  oneHundThou();
  

I am aware that there is another post that is similar to this, however, my answer could not be answered on that post.

Full code is here: https://code.sololearn.com/Wwdw59oenFqB
Please help! Thank you. (:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅淡淡 2025-02-11 09:41:30

不要存储图像,而是尝试存储innerhtml,然后在文档加载上创建图像:

function oneHundThou() {
  countvar.innerHTML = localStorage.getItem('clickCount') ? localStorage.getItem('clickCount') : 0; //before if
  //original code
  localStorage.setItem('clickCount', countvar.innerHTML); //instead of doc.cookie
}

window.addEventListener('DOMContentLoaded', (event) => {
    oneHundThou();
});

或者,如果您不在乎clickcounter可能会初始化到null,则可以删除? :,然后放置

countvar.innerHTML = localStorage.getItem('clickCount');

编辑:用countvar变量缩短代码

Instead of storing the image, try storing the innerHTML, then create the image on document load:

function oneHundThou() {
  countvar.innerHTML = localStorage.getItem('clickCount') ? localStorage.getItem('clickCount') : 0; //before if
  //original code
  localStorage.setItem('clickCount', countvar.innerHTML); //instead of doc.cookie
}

window.addEventListener('DOMContentLoaded', (event) => {
    oneHundThou();
});

or, if you don't care that clickCounter may initialize to null, you can remove the ? : and just put

countvar.innerHTML = localStorage.getItem('clickCount');

Edit: shortened code with the countvar variable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文