如何使用JavaScript存储新创建的元素或cookie?
我正在制作一个闲置的点击器游戏,以娱乐,一切都很好,直到遇到问题。
我基本上想发生的事情是,当单击图像并且点击元素元素超过一个时,创建了新的图像元素。这里没问题,主要问题是保存图像。如果用户刷新页面,我希望创建的元素仍然存在。我尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要存储图像,而是尝试存储
innerhtml
,然后在文档加载上创建图像:或者,如果您不在乎clickcounter可能会初始化到
null
,则可以删除? :
,然后放置编辑:用
countvar
变量缩短代码Instead of storing the image, try storing the
innerHTML
, then create the image on document load:or, if you don't care that clickCounter may initialize to
null
, you can remove the? :
and just putEdit: shortened code with the
countvar
variable