html5的localStorage使用示例

html5的localStorage使用示例,第1张

概述html5的localStorage使用示例

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

<!DOCTYPE HTML><head><Meta charset="UTF-8"><Title>SO EASY!</Title><style type="text/CSS">    HTML,body {        background-color: #262;        color: #fff;        Font-family: helvetica,arial,sans-serif;        margin: 0;        @R_419_5095@: 0;        Font-size: 11pt;    }       </style><script lang='text/JavaScript'>    window.addEventListener("load",eventwindowLoaded,false);    var entrIEs = [];    var curIndex = -1;    function eventwindowLoaded()    {        loadEntrIEs(1);        showNext();        var dayselection = $("dayselection");        dayselection.addEventListener("change",daysSelectionChanged,false);       }    function daysSelectionChanged(e)    {        var target = e.target;        loadEntrIEs(target.value);        clearTextarea();        curIndex = -1;        showNext();        log('总共'+entrIEs.length+'个,当前第'+(curIndex+1)+'个');    }    function loadEntrIEs(days)    {        var Now = new Date().getTime();        var arr = [];        for(var i=0; i<localstorage.length; i++)        {            var time = localstorage.key(i);            var daysBetween = (Now - time)/24/60/60/1000;            // console.log(daysBetween);            if (daysBetween <= days)            {                var value = JsON.parse(localstorage.getItem(time));                var entry = {time: time,data: value};                arr.push(entry);            }        }        entrIEs = arr;    }    function saveEntry()    {        var data_en = $('en').value;        var data_zh = $('zh').value;        var data = {en: data_en,zh: data_zh};        var time = new Date().getTime();        if (data_en =='' && data_zh == '') // Todo: 正则        {            alert('必须输入中英文表达!');        }        else        {            localstorage.setItem(time,JsON.stringify(data));                           // Update UI            log('保存成功!');            // ReLoad entrIEs            loadEntrIEs($('dayselection').value);            // Recovery current entry            curIndex --;            showNext();        }    }    function deleteEntry()    {        var currentEntry = entrIEs[curIndex];        if (currentEntry)        {            localstorage.removeItem(currentEntry.time);            // Update UI            log('删除成功!');            // Reload entrIEs            loadEntrIEs($('dayselection').value);            // Go to next entry            curIndex --;            showNext();        }    }    function clearStorage()    {        localstorage.clear();        log('数据初始化完成!');    }    function showNext()    {           if (curIndex +1 <= entrIEs.length-1)        {            curIndex ++;            var entry = entrIEs[curIndex];            showtextarea(entry.data);            log('总共'+entrIEs.length+'个,当前第'+(curIndex+1)+'个');        }    }    function showPrevIoUs()    {        if (curIndex-1 >= 0)        {            curIndex --;            var entry = entrIEs[curIndex];                      showtextarea(entry.data);            log('总共'+entrIEs.length+'个,当前第'+(curIndex+1)+'个');        }    }    function showtextarea(data)    {        var target_en = $('en');        var target_zh = $('zh');        target_en.value = data.en;        target_zh.value = data.zh;    }    function clearTextarea()    {        $('en').value = '';        $('zh').value = '';    }    function $(ID)    {        return document.getElementByID(ID);    }    function log(msg)    {        var target = document.getElementByID('msg');        target.INNERHTML = msg;    }</script></head><body><div >    回顾:<select ID="dayselection">      <option value="1"> 1天内 </option>      <option value="0.04166666667"> 1小时内 </option>      <option value="3"> 3天内 </option>      <option value="7"> 7天内 </option>      <option value="14"> 14天内 </option>      <option value="30"> 1月内 </option>      <!-- <option value="">  </option> -->    </select>    </tr>    <input type="button" ID="prev" value="上一个" onclick="showPrevIoUs();">    <input type="button" ID="next" value="下一个" onclick="showNext();">    <hr> 英语表达:<br/>    <textarea ID="en" cols="40" rows="6"></textarea>    <hr> 中文解释:<br/>    <textarea ID="zh" cols="40" rows="6"></textarea><br/>    <input type="button" ID="save" value="保存内容" onclick="saveEntry();">    <input type="button" ID="delete" value="删除内容" onclick="deleteEntry();">         <hr> <p ID="msg"></p><div></body></HTML>

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的html5的localStorage使用示例全部内容,希望文章能够帮你解决html5的localStorage使用示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1097610.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存