将表单值保存到localstorage
我已经研究JS了一段时间,所以在某个时候,A无法完成我的老师给我的作业,因此它将创建一个输入以填充3个选择器以更改文本的某些属性。
我要做的是:
每当用户关闭页面并再次打开页面时 - 将localstorage中的值应用于表单中,并将样式应用于元素。 IE:
字体:roboto
颜色:绿色
fontsize:24px
我应该怎么做,为什么我的代码错了?
这是代码:
function iterator() {
for (let i = 16; i <= 30; i++) {
let fontsSize = document.getElementById("fontsSize");
let CreateElmt = document.createElement("option");
fontsSize.append(CreateElmt);
CreateElmt.textContent = i + "px";
CreateElmt.value = i + "px";
}
}
iterator();
function finalItreator() {
let parent = document.querySelector(".daddyDiv");
let div = document.querySelector(".paragragh");
let Fonts = document.getElementById("Fonts");
let Colors = document.getElementById("Colors");
let fontSizes = document.getElementById("fontsSize");
parent.addEventListener("input", (e) => {
window.localStorage.setItem("Font", Fonts.value);
window.localStorage.setItem("Color", Colors.value);
window.localStorage.setItem("FontSize", fontSizes.value);
div.style.fontFamily = Fonts.value;
div.style.color = Colors.value;
div.style.fontSize = fontSizes.value;
for (i = 0; i < Fonts.children.length; i++) {
if (Fonts.children[i].value === window.localStorage.getItem("Font")) {
Fonts.forEach((e) => {
e.removeAttribute("selected");
});
Fonts.children[i].classList.add("active");
}
if (Fonts.children[i].classList.contains("active")) {
Fonts.children[i].setAttribute("selected", "");
}
}
});
}
finalItreator();
.paragragh {
margin: 10px auto;
width: 400px;
min-height: 300px;
background-color: #ddd;
text-align: center;
padding-top: 30px;
}
<form class="daddyDiv">
<select name="Fonts" id="Fonts">
<option value="Open Sans">Open Sans</option>
<option value="Cairo">Cairo</option>
<option value="Roboto">Roboto</option>
</select>
<select name="Colors" id="Colors">
<option value="Black">Black</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Red">Red</option>
<option value="Purple">Purple</option>
</select>
<select name="fontsSize" id="fontsSize"></select>
<div class="paragragh" contenteditable="true"></div>
</form>
I have been studying JS for a while so at some point a couldn't complete the assignment that my teacher gave me so its all about to create a input to fill with 3 selectors to change some properties of the text.
What I'm trying to do is:
whenever the user he closes the page and opens it again - apply the values from localStorage back into the form, and apply the styles to an element. I.e:
Font: Roboto
Color: Green
fontSize: 24px
How I am supposed to do it, and why my code is wrong?
Here's the code :
function iterator() {
for (let i = 16; i <= 30; i++) {
let fontsSize = document.getElementById("fontsSize");
let CreateElmt = document.createElement("option");
fontsSize.append(CreateElmt);
CreateElmt.textContent = i + "px";
CreateElmt.value = i + "px";
}
}
iterator();
function finalItreator() {
let parent = document.querySelector(".daddyDiv");
let div = document.querySelector(".paragragh");
let Fonts = document.getElementById("Fonts");
let Colors = document.getElementById("Colors");
let fontSizes = document.getElementById("fontsSize");
parent.addEventListener("input", (e) => {
window.localStorage.setItem("Font", Fonts.value);
window.localStorage.setItem("Color", Colors.value);
window.localStorage.setItem("FontSize", fontSizes.value);
div.style.fontFamily = Fonts.value;
div.style.color = Colors.value;
div.style.fontSize = fontSizes.value;
for (i = 0; i < Fonts.children.length; i++) {
if (Fonts.children[i].value === window.localStorage.getItem("Font")) {
Fonts.forEach((e) => {
e.removeAttribute("selected");
});
Fonts.children[i].classList.add("active");
}
if (Fonts.children[i].classList.contains("active")) {
Fonts.children[i].setAttribute("selected", "");
}
}
});
}
finalItreator();
.paragragh {
margin: 10px auto;
width: 400px;
min-height: 300px;
background-color: #ddd;
text-align: center;
padding-top: 30px;
}
<form class="daddyDiv">
<select name="Fonts" id="Fonts">
<option value="Open Sans">Open Sans</option>
<option value="Cairo">Cairo</option>
<option value="Roboto">Roboto</option>
</select>
<select name="Colors" id="Colors">
<option value="Black">Black</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Red">Red</option>
<option value="Purple">Purple</option>
</select>
<select name="fontsSize" id="fontsSize"></select>
<div class="paragragh" contenteditable="true"></div>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
缓存元素您计划事先重复使用。
const
当变量值不会通过应用程序的寿命变化时id
,如果您已经使用name
name迭代器
如果其作业显然是用&lt; option&gt;
s填充选择框。CreateFontSizeOptions
似乎是一个更好的选择。finalItreator
这没什么意思。将您的功能名称描述为简洁,有意义且尽可能短。
“输入”
事件分配给类选择器,即:.command
,该分配给您的选择(或其他纯)元素。name
哪个值完全是预期的CSS属性,即骆驼,即:fontfamily
或fontsize
(由于出于安全原因堆栈溢出摘要不接受iframe访问localstorage的访问,因此这里是 /a> )
代码:
Cache the elements you plan to reuse beforehand.
const
when the variable value will not change through the life of your appid
if you already usename
iterator
if its job is clearly to populate a Select box with<option>
s.createFontSizeOptions
seems like a better choice.finalItreator
it means nothing.Describe your function names as concisely, meaningfully, and short as possible.
"input"
Event to a class selector, i.e:.command
which is assigned to your Select (or other imput) elements.name
which value is exactly the expected CSS property as camelCase, i.e:fontFamily
orfontSize
(Since Stack Overflow Snippets for security reasons doesn't accept access to localStorage from Iframe, here's a live jsFiddle demo)
Code: