如何在不刷新页面的情况下更新显示框

How do I update my display box without having to refresh the page?

本文关键字:情况下 更新 显示 刷新      更新时间:2023-09-26

我有一个脚本,它计算unicreatures.com上的步数,并添加了一个按钮来清除计数。它起作用了,但我不高兴的是,我的显示框在重新加载页面之前不会更新新的计数。有没有一种方法可以在清除计数后更新我的盒子内容,而不必重新加载页面?

var clearTotal= confirm("Do you really want to clear your step count?");
if (clearTotal== true) {
  localStorage.steps=0
  alert("yes");
}
else {
  alert("no");
}
}, true )  

如果有人能为我指明正确的方向,我会很高兴。我花了几个小时寻找答案,但也许我只是用错了搜索词。

我不使用JQuery,所以请将所有信息保存到JavaScript

您可以清除回调中的显示或编写闭包。

一个非常简单的方法是,当您检查本地存储是否清除时,将显示区域的值设置为

var myTextField = document.getElementById('the_display_area_id');    
var clearTotal= confirm("Do you really want to clear your step count?");
if (clearTotal== true) {
    localStorage.steps=0
    myTextField.value() = ""; 
}else{
    alert("no");
}

此外,使用类似库的放大器进行本地存储,将更容易处理跨浏览器的细微差别

我希望早点看到它。

最简单的方法是更改CCD_5 的值

if (clearTotal== true) {
  localStorage.steps=0
  box.innerHTML ='<div><p> ' +
  "You have taken "+localStorage.steps+" steps total" +
  '</br><button type="button" button id="clear">Clear total steps...</button></p></div>';
  document.body.appendChild( box );
  alert("yes");
}