h5 本地存储和读取信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了h5 本地存储和读取信息相关的知识,希望对你有一定的参考价值。

参考:http://killtyz.com
$(document).ready(function () { $(‘#add-input‘).focus(); $(‘#add-input‘).bind(‘keypress‘,function(event){ var value = $(this).val(); if( event.keyCode == "13" && value ) { var time = (new Date()).getTime(); addItem(time, value, false); store(time, value, false); $(this).val(‘‘); } }); $(‘#list‘).click( function(event){ var target = $(event.target); if (target.hasClass(‘delete‘)) { target.parents(‘.list-group-item‘).remove(); localStorage.removeItem(target.parents(‘.list-group-item‘).attr(‘data-time‘)); return false; } if (!target.hasClass(‘list-group-item‘)) { target = $(target).parents(‘.list-group-item‘); } var check = target.children(‘.check‘).not(‘.checked‘); if (check.length) { changeStatus(target.attr(‘data-time‘), true); check.addClass(‘checked‘); check.next(‘.value‘).addClass(‘checked‘); check.next(‘.value‘).next(‘.delete‘).show(); } else{ changeStatus(target.attr(‘data-time‘), false); target.children(‘.check.checked‘).removeClass(‘checked‘); target.children(‘.value.checked‘).removeClass(‘checked‘); target.children(‘.delete‘).hide(); } return false; }); store(0, ‘KillTYZ 基础 Todo-List 功能实现‘, true); store(1, ‘浏览器本地存储 功能实现,请大胆刷新页面!‘, true); for(x in localStorage) { var obj = JSON.parse(localStorage.getItem(x)); addItem(obj.time, obj.value, obj.checked); } }); function store(time, value, checked) { localStorage.setItem(time, stringify(time, value, checked)); } function changeStatus(time, checked) { var obj = JSON.parse(localStorage.getItem(time)); obj.checked = checked ? true : false; localStorage.setItem(time, JSON.stringify(obj)); } function stringify(time,value,checked) { var obj = new Object; obj.time = time; obj.value = value; obj.checked = checked; return JSON.stringify(obj); } function addItem(time, value, checked) { var node = checked ? ‘<a href="#" class="list-group-item" data-time="‘+time+‘"><span class="check checked"></span><span class="value checked">‘+value+‘</span><span class="delete glyphicon glyphicon-trash"></span></a>‘ : ‘<a href="#" class="list-group-item" data-time="‘+time+‘"><span class="check"></span><span class="value">‘+value+‘</span><span class="delete glyphicon glyphicon-trash" style="display: none;"></span></a>‘; $(‘#list‘).prepend(node); }

以上是关于h5 本地存储和读取信息的主要内容,如果未能解决你的问题,请参考以下文章

H5本地存储(转)

H5本地存储

H5本地存储:sessionStorage和localStorage

小程序跳转进入H5页面,全局读取token

H5本地储存Web Storage

app扫码打开h5链接如何获取app本地存储数据