我有一个复选框输入,默认情况下处于选中状态,它可以切换页面上元素的可见性。我希望用户在刷新页面时保存他们的选项。
var c = document.createElement('input')
c.setAttribute("type", "checkbox")
c.checked = true
c.onclick = function(){
if(c.checked) element.style.display = 'block'
if(c.checked == false) element.style.display = 'none'
}
/* This code below saves the state of the checkbox,
but never fires the event or updates the element */
c.checked = (localStorage.getItem('cchecked')== 'true');
c.onchange = function(){
localStorage.setItem('cchecked', c.checked)
}
答案 0 :(得分:0)
一个更完整的示例作品..
prepareForReuse