[英]Javascript ES6: setting <h1> with local storage and form submission
I am trying to set the element #welcome_banner
by using a function in javascript that takes a form submission and sets it to localStorage
and then pulls that info and sets it onto an <h1>
tag. 我试图通过使用javascript中的函数来设置元素
#welcome_banner
,该函数接受表单提交并将其设置为localStorage
,然后提取该信息并将其设置到<h1>
标记上。 Apparently it works but only changes the tag for a millisecond and then it disappears! 显然,它可以工作,但是只更改标签一毫秒,然后它消失了! I have tried doing the
.innerHTML
setting in various places inside and outside of the function clickHandler()
and in the main body of the script. 我尝试在
function clickHandler()
内部和外部以及脚本主体中的各个位置进行.innerHTML
设置。 I am certain this is something superbasic I am missing. 我敢肯定这是我所缺少的基础知识。
<!DOCTYPE html>
<html>
<head>
</head>
<script >
//set display name to form submission, set welcome banner to display name
function clickHandler () {
document.querySelector('#display_name').onsubmit = function() {
localStorage.setItem('dn', dn);
document.querySelector('#welcome_banner').innerHTML=changeWelcomeBanner;
}
};
function changeWelcomeBanner () {
var dn = localStorage.getItem('#dn').value;
var welcomeBanner = document.getElementById('#welcome_banner');
welcomBanner.innerHTML = `Hello ${dn}`;
}
</script>
<title>Project 2</title>
<body style="background-color:#ff3300;">
<h1 id="welcome_banner"></h1>
<form id="display_name">
<input id="dn" autocomplete="off" autofocus placeholder="" type="text">
<button>set display name</button>
</body>
</html>
The steps to do this are as follows: 为此,请按照以下步骤操作:
localStorage.setItem('dn', dn);
localStorage.setItem('dn', dn);
document.addEventListener("DOMContentLoaded", function(event) { // code to check local storage goes here... });
document.addEventListener("DOMContentLoaded", function(event) { // code to check local storage goes here... });
var dn = localStorage.getItem('dn').value;
var dn = localStorage.getItem('dn').value;
and if there is a value there, set the innerHtml of the <h1>
to that value, like you're doing here: welcomBanner.innerHTML = 'Hello ${dn}';
<h1>
的innerHtml设置为该值,就像您在此处所做的那样: welcomBanner.innerHTML = 'Hello ${dn}';
I think you might have had a stray # character in your localStorage.getItem call that I removed in the steps above. 我认为您可能在我在上述步骤中删除的localStorage.getItem调用中有一个流浪#字符。 You might also want to have default text you post if there's nothing found in localStorage when you check.
如果您在检查时在localStorage中找不到任何内容,则可能还需要发布默认文本。
Here's a simplified example: If you are calling a function to get the value from Local Storage, be sure you have a return
这是一个简化的示例:如果您要调用一个函数以从本地存储中获取值,请确保已
return
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <h1 id="welcome"></h1> <input type="text" id="something"> <button type="button" id="click">CLICK ME</button> <script> document.getElementById("click").addEventListener("click", function(){ var x=document.getElementById("something").value; document.getElementById("welcome").innerHTML=x; localStorage.setItem('x', JSON.stringify(x)); document.getElementById("welcome").innerHTML = getData(); }); function getData(){ var retrieve=localStorage.getItem('x'); return JSON.parse(retrieve); //Now return the value } </script> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.