localstorage刷新,localstorage数据丢失非常不多说了,直接给大家贴代码了,具体代码如下所示: <!doctype html><html lang="en"><head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <title>Document</title> <script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script></head><body> <ul> <li>555</li> <li>555</li> <li>555</li> <li>555</li> </ul> <button>button</button> <script type="text/javascript"> var arrary_li = document.querySelectorAll("li"); var num; $('button').on('click', function () { $('li').last().addClass('active') for (var i = 0; i < arrary_li.length; i++) { if (arrary_li[i].className === 'active') { num = i; } } localStorage.setItem('num', num) location.reload() }) var getNum = parseInt(localStorage.getItem('num')); $('li').eq(getNum).addClass('active'); </script></body></html> 以上所述是小编给大家介绍的JS中利用localStorage防止页面动态添加数据刷新后数据丢失,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对wanshiok.com网站的支持! localstorage刷新,localstorage数据丢失
|