如何创建本地存储? [英] How to create local storage?
问题描述
我的网站上有一些选项卡,并且在该选项卡中,信息卡和右侧的复选框用不同的颜色突出显示了该卡.我如何才能做到,即使刷新页面或将页面作为链接发送,也可以保存onclick突出显示内容?此处的完整代码: http://codepen.io/johnsonshara/pen/obGjGN (下面的代码不是在代码笔版本中)
My website has tabs and within that tab, info cards and a checkbox on the right hand side that highlights the card in a different colour. How can I makes it so that onclick the highlighting is saved even if the page is refreshed or if the page is sent as a link? Full code here: http://codepen.io/johnsonshara/pen/obGjGN (below code not in code pen version)
我尝试过的事情:
function save () {
var fieldValue = document.getElementById('like').value;
localStorage.setItem('checkbox', 'fieldValue');
}
function load () {
var storedValue = localStorage.getItem('checkbox');
if(storedValue){
document.getElementById('like').value = storedValue;
}
}
还有:
function save() {
if(typeof(Storage) !== "undefined") {
if (localStorage.setItem) {
localStorage.setItem = document.getElementById("like");
} else {
localStorage.getItem = .hasClass.apply;
}
document.getElementById("like").innerHTML = localStorage.toString;
} else {
document.getElementById("like").innerHTML = "Sorry, an error occured.";
}
}
html
<input type="checkbox" class="faChkRnd" onclick="save()" id="like" >
推荐答案
查看以下链接,了解如何使用本地存储:
Check out the following link to see how you can use the local storage:
http://www.w3schools.com/html/html5_webstorage.asp
如何运行的快速示例代码:
Quick example code of how it would work:
// Put the object into storage
localStorage.setItem('testObject', "testing");
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
使用上面的示例代码的示例:
Example using your example code above:
alert(load());
$(document).on("click", "#clickME", function(){
save();
});
function save ()
{
var fieldValue = "testString";
localStorage.setItem('checkbox', fieldValue);
}
function load ()
{
var storedValue = localStorage.getItem('checkbox');
if(storedValue)
{
console.log("VALUE :" + localStorage.getItem('checkbox'));
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="clickME">Click me</button>
这篇关于如何创建本地存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!