Javascript 返回页面时,脚本将被重置
我已经在Javascript中构建了一个函数,允许关注输入字段(类似于Google帐户登录)。脚本的工作原理如下:Javascript 返回页面时,脚本将被重置,javascript,html,input,Javascript,Html,Input,我已经在Javascript中构建了一个函数,允许关注输入字段(类似于Google帐户登录)。脚本的工作原理如下: const reg_inputs = document.querySelectorAll(".reg-input"); function addcl() { const parent = this.parentNode.parentNode; parent.classList.add("focus"); } function remcl() { const par
const reg_inputs = document.querySelectorAll(".reg-input");
function addcl() {
const parent = this.parentNode.parentNode;
parent.classList.add("focus");
}
function remcl() {
const parent = this.parentNode.parentNode;
if (this.value == "") {
parent.classList.remove("focus");
return false;
}
}
reg_inputs.forEach((input) => {
input.addEventListener("focus", addcl);
input.addEventListener("blur", remcl);
return true;
});
输入字段中有一个标题。只要单击对象并写入内容,标题就会缩小并显示在字段上方。如果完全删除文本并单击输入字段,焦点将消失
在脚本中,它如下所示:
const reg_inputs = document.querySelectorAll(".reg-input");
function addcl() {
const parent = this.parentNode.parentNode;
parent.classList.add("focus");
}
function remcl() {
const parent = this.parentNode.parentNode;
if (this.value == "") {
parent.classList.remove("focus");
return false;
}
}
reg_inputs.forEach((input) => {
input.addEventListener("focus", addcl);
input.addEventListener("blur", remcl);
return true;
});
问题是,当用户单击一个按钮时,他会被带到另一个页面。如果用户现在返回到旧页面,标题将再次出现在输入字段中,并覆盖正常输入的文本。这看起来很不友好,应该修复。我认为脚本将在返回时重置。有人能帮我吗?网页是无状态的 这意味着,如果您在网页上运行脚本,然后离开该页面,然后返回该页面,则该页面将被加载,就像该脚本尚未运行一样(因为它尚未运行) 但是,有几种方法可以向浏览器报告状态:
- 使用锚定
(例如#
)example.com/my page.html#my state
- 使用查询字符串(例如
)example.com/my page.html?state=my state
- 饼干
- 使用
(或者webStorage
或者localStorage
)sessionStorage
有关
webStorage
webStorage API
创建了一个简单的键值存储,它在页面重新加载之间以及同一站点上的不同页面之间保持不变
localStorage
和sessionStorage
之间的区别在于,当您离开网站时,任何sessionStorage
键和值都将被删除
如果您愿意,可以将webstorageapi
看作(不完全是,但是)有点像“客户端cookies”
使用起来比较无痛
您需要记住的唯一一件事是,每个Web存储
条目必须是字符串
(而不是数字
、数组或对象
)
甚至这也不是什么大问题,因为我们可以使用JSON
与数组和对象通信
要将字符串值添加到
localStorage
,请使用:
localStorage.setItem('myKey', 'myStringValue');
let myValue = localStorage.getItem('myKey');
if (localStorage.getItem('myKey') !== null)
localStorage.removeItem('myKey');
localStorage.clear();
要从localStorage
检索该字符串值,请使用:
localStorage.setItem('myKey', 'myStringValue');
let myValue = localStorage.getItem('myKey');
if (localStorage.getItem('myKey') !== null)
localStorage.removeItem('myKey');
localStorage.clear();
要检查localStorage
中是否存在密钥,请使用:
localStorage.setItem('myKey', 'myStringValue');
let myValue = localStorage.getItem('myKey');
if (localStorage.getItem('myKey') !== null)
localStorage.removeItem('myKey');
localStorage.clear();
要从localStorage
中删除字符串值,请使用:
localStorage.setItem('myKey', 'myStringValue');
let myValue = localStorage.getItem('myKey');
if (localStorage.getItem('myKey') !== null)
localStorage.removeItem('myKey');
localStorage.clear();
要从localStorage
中删除所有字符串值,请使用:
localStorage.setItem('myKey', 'myStringValue');
let myValue = localStorage.getItem('myKey');
if (localStorage.getItem('myKey') !== null)
localStorage.removeItem('myKey');
localStorage.clear();