触发事件后如何在本地存储中设置复选框状态

时间:2019-01-05 22:15:44

标签: javascript local-storage game-development

我有一个复选框输入,默认情况下处于选中状态,它可以切换页面上元素的可见性。我希望用户在刷新页面时保存他们的选项。

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)
}

1 个答案:

答案 0 :(得分:0)

一个更完整的示例作品..

prepareForReuse