简短的回答:是的。存储在 JS 文件中的每个变量都会丢失,或者说,每次重新加载页面或更改页面时都会创建一个新副本。 Window.location.href.要在重新加载之间保留数据或使用变量的值,您应该使用客户端数据存储,例如sessionStorageorlocalStorage或cookies。例如,让我们说使用可以使用的持久变量的值let persist = "My data";// Store it in localstorage localStorage.set('persist', persist);// Get the value of your saved variable to use it elsewhere let persist = locastorage.get("persist");//To delete the variable from storage localStorage.removeItem('persist')// To clear the entire storagelocalStorage.clear()语法与sessionStorage存储的值相同,但仅sessionStorage在页面会话期间可用。有关DOM 存储的更多信息,您可以在这里阅读。