我有一个添加元素功能,完美无缺。我只是不知道如何将此功能添加到localstorage中。很新,你能解释一下吗?
$(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));
});
&#13;
答案 0 :(得分:2)
你可以使用什么。您可以将标记保存到本地存储,然后将其检索并将其添加回DOM。
请注意,此代码段不会在答案范围内运行,因为沙盒框架无法访问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);
}
&#13;
<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>
&#13;