简体   繁体   English

如何将 jQuery 保存到本地存储

[英]How to save jQuery into local storage

I have an add element function, which works perfectly.我有一个添加元素功能,效果很好。 I just have no idea how to add this function into localstorage.我只是不知道如何将此功能添加到 localstorage 中。 Very new, could you please give a explanation?很新,能解释一下吗?

 $(document).on('click', 'li#order_open', function(){ $(this).before('<li><a href="Menu.php">New restaurant</a></li>'); // add to localsorage? var order_open = $('li#order_open').html(); localStorage.setItem('li#order_open', order_open); localStorage.setItem($(this)); });

What you have could be used.你所拥有的都可以使用。 You can save the markup to local storage, and later retrieve it and add it back to the DOM.您可以将标记保存到本地存储,稍后检索它并将其添加回 DOM。

Note that this snippet will not run within the answer, as the sandboxed frame cannot access localStorage .请注意,此代码段不会在答案中运行,因为沙盒框架无法访问localStorage

 function appendFromLocal() { //This is just for demonstration purposes $(document.body).append(localStorage.getItem('div')); //This is what could be used to reload the element on page load //$('div').html(localStorage.getItem('div')) } function addNewItem() { $('ul').append('<li><a href="Menu.php">New restaurant</a></li>'); var order_open = $('div').html(); localStorage.setItem('div', order_open); }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button onclick="appendFromLocal()">Append From Local Storage</button> <button onclick="addNewItem()">Add New Item</button> <div> <ul> <li><a href="Menu.php">New restaurant</a></li> </ul> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM