简体   繁体   English

一键两种功能

[英]One button with two different functions

I was hoping to get some help, I'm planning on creating a button clicker website.我希望得到一些帮助,我正计划创建一个按钮点击器网站。 Wherein one button can create two events:其中一个按钮可以创建两个事件:

  1. Store a counter permanently ("clicks") (When website is refreshed, progress will NOT be lost) - I was able to do this永久存储一个计数器(“点击”)(当网站刷新时,进度不会丢失)-我能够做到这一点
  2. Amount of clicks ("myclicks") (When website is refreshed, progress WILL be lost)- This one I have no idea how, I've been trying to find solutions online, but I still can't understand how to do it.点击量(“myclicks”)(当网站刷新时,进度会丢失)-这个我不知道如何,我一直在网上寻找解决方案,但我仍然不明白该怎么做。

HTML HTML

   <!DOCTYPE html>
   <html lang="en">
   
   <head>
       <title>Button Clicker</title>
   </head>
   
   <body>

       <table class="center">
   
            <!-- Stored Clicks -->
           <tr align="center">
               <td><h1 id="clicks">0</h1></td>
           </tr>
           <!-- How many times you've clicked "myclicks" -->
           <tr align="center">
               <td><h2 id="myclicks">0</h2></td>
           </tr>
           <tr align="center">
               <td><p><button id="clickButton">Click Here</button></p></td>
           </tr>
   
           <!----------------------------------------- RESET ----------------------------------------->
           <tr align="center">
               <td><p><button id="resetBtn">Reset</button></p></td>
           </tr>
   
       </table>    
   
   
   <script src="./index.js"></script>
   
   
   
   </body>
   
   </html>

JAVASCRIPT JAVASCRIPT

// STORED CLICKS
var clicks = 0;

function initiateClicks() {
    var clickStr = localStorage.getItem("clicks");
    if(clickStr == undefined){
        localStorage.setItem("clicks", 0);
        clicks = 0;
    }else{
        clicks = parseInt(clickStr);   
    }
    document.getElementById("clicks").innerHTML = clicks;
}

function doClick() {
    clicks += 1;
    localStorage.setItem("clicks", clicks);
    document.getElementById("clicks").innerHTML = clicks;
}

document.getElementById("clickButton").onclick = doClick;

// MY CLICKS



// RESET

document.getElementById("resetBtn").onclick = function(){
    clicks=0;
    document.getElementById("clicks").innerhtml = count;
}


I have tried on javascript for "myclicks":我在 javascript 上试过“myclicks”:

// MY CLICKS
var myclicks = 0;
function myClick() {
    clicks += 1;
    document.getElementById("myclicks").innerHTML = myclicks;
}

document.getElementById("clickButton").onclick = myClick;

Your problem is you not parse to save in localStorage你的问题是你没有解析保存在 localStorage

  • Exemple:例子:

const valueToStore = JSON.stringify(localeStorage.setItem("some","value"))
const getValue = JSON.parse(localeStorage.setItem("some"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM