我現在正在編寫一個簡單的香草JavaScript待辦應用程序。一切看起來都很好,但是當涉及到將數據保存在新的localStorage中時,我無法做到,從未使用過localStorage,我讀過很多文章,當我嘗試使用簡單的東西時,但我無法讓我的應用程序正常工作。我想保存輸入的數據等刷新,它不會消失,現在的方式...這是鏈接到應用程序用localStorage做的應用程序
****我想要與VANILLA JAVASCRIPT,而不是JQUERY ****
所以基本上,如果用戶進行多次待辦事項,當刷新,我想一切都留下來,像在圖像的打擊。
HTML:
<body>
<div class="holder">
<div>
<input type="text" />
</div>
<button id="add">Add</button>
<div class="results"></div>
</div>
</body>
JS:
document.addEventListener('DOMContentLoaded',function(){
let holder = document.querySelector('.holder'),
addBtn = document.querySelector('#add'),
removeBtn = document.querySelector('#remove'),
resultBox = document.querySelector('.results');
let input = document.getElementsByTagName('input')[0];
function setAttributes(element,attrs){
for(let i in attrs){
element.setAttribute(i,attrs[i]);
}
}
setAttributes(input,{
'placeholder' : 'Enter To-Do',
'name' : 'todo'
});
function addtoBtn(){
addBtn.addEventListener('click',function(event){
if(input.value !== ''){
let openingDiv = '<div class="todo"><span>';
let closingDiv = '</span><button class="edit">Edit</button><button class="save">Save</button><button class="removeBtn">Remove</button></div>';
resultBox.innerHTML += openingDiv + input.value + closingDiv;
input.value = '';
event.preventDefault();
} else{
alert('Enter To-do!');
}
let innerBtn = document.querySelectorAll('.removeBtn');
let editBtn = document.querySelectorAll('.edit');
let saveBtn = document.querySelectorAll('.save');
for(let collection = 0 ; collection < saveBtn.length ; collection++){
saveBtn[collection].style.display = "none";
}
function removeToDo(){
this.parentNode.style.display = 'none';
}
for(let k = 0 ; k < innerBtn.length ; k++){
innerBtn[k].addEventListener('click',removeToDo);
}
function startContentEdit(){
this.previousSibling.contentEditable = true;
this.previousSibling.style.padding = "10px";
this.previousSibling.style.boxShadow = "0 0 15px #fff";
this.previousSibling.style.borderRadius = "10px";
this.previousSibling.style.transition = "all .4s";
this.style.display = "none";
for(let el = 0 ; el < saveBtn.length ; el++){
this.nextSibling.style.display = 'inline-block';
}
}
for(let j = 0 ; j < editBtn.length ; j++){
editBtn[j].addEventListener('click',startContentEdit);
}
function saveContentEdit(){
this.style.display = 'none';
for(let j = 0 ; j < editBtn.length ; j++){
this.previousSibling.style.display = "inline-block";
this.parentNode.firstElementChild.style.display = "block";
this.parentNode.firstElementChild.contentEditable = false;
this.parentNode.firstElementChild.style.padding = "0px";
this.parentNode.firstElementChild.style.boxShadow = "0 0 0";
}
}
for(let x = 0 ; x < saveBtn.length ; x++){
saveBtn[x].addEventListener('click',saveContentEdit);
}
});
}
addtoBtn();
});
正如我所說的,我看到很多文章,但我找不到任何答案,我的解決方案。 對不起,但這是我的第一個JavaScript應用程序,幾乎所有的工作:D和我有點興奮,所以我想使它充分工作。
簡歷:
是否有可能用的localStorage或sessionStorage的,保存新生成的
<div class="todo></div>
容器?
存儲* HTML元素*,而不是它背後的數據聽起來像一個壞主意。這是很多代碼,可以證明堆棧溢出問題太多了。你能否更詳細地澄清你所困擾的事情,並顯示與問題相關的代碼? –