我尝试使用jquery从localStorage中删除单个项目。在这里,我将listivew中的每个项目添加复选框以删除已选中的复选框,这些项目必须从localStorage中删除,但它无法正常工作。
更新代码:
$(document).ready( function() {
$("#section_list").on('click', 'a', function() {
var item = $(this).find('h2').text();
var desc = $(this).find('p').text();
var html = '<p></br><p class="description">' + desc + '</p>';
$('.addToFavoritesDiv').click(function() {
var url = $(location).attr('href');
if (!supportLocalStorage())
{
item = 'No support';
}
else
{
try
{
localStorage.setItem('autosave' + url, item + html);
}
catch (e)
{
if (e == QUOTA_EXCEEDED_ERR)
{
alert('Quota Exceeded');
}
}
}
});
});
$('#fav').click(function() {
fromStorage();
$("#favoritesList").listview('refresh');
});
$(document).on('click', '#edit', function() {
fromGetStorage();
});
});
function supportLocalStorage()
{
return typeof(Storage) !== 'undefined';
}
function fromStorage()
{
$("#favoritesList").empty();
$("#favoritesList").listview('refresh');
for (var i = 0; i < localStorage.length; i++) {
var url = localStorage.key(i);
var item = localStorage.getItem(url);
$("#favoritesList").append('<li id="add"><a href="' + url + '" style="text-decoration:none;color:black">' + item + '</a></li>').attr('url', url);
}
$("#favoritesList").listview('refresh');
}
function fromGetStorage()
{
$("#favoritesList").empty();
$("#favoritesList").append('<input type="checkbox" name="all" id="select" />Select all</br>');
$("#fav .ui-listview-filter").remove();
for (var i = 0; i < localStorage.length; i++) {
var url = localStorage.key(i);
var item = localStorage.getItem(url);
$("#favoritesList").append('<li id="add"><div><input type="checkbox" name="check" id="check"> <a href="' + url + '" style="text-decoration:none;color:black">' + item + '</a></div></li>').attr('url', url);
$("#favoritesList").listview('refresh');
}
$("#select").click(function(event){
event.stopPropagation();
});
$('#select').change(function() {
if($("#select").is(':checked')) {
$("#add #check").prop("checked", true);
$('#empty').on('click', function() {
localStorage.clear();
$("#favoritesList").listview('refresh');
});
}
else {
$("#add #check").prop("checked", false);
}
});
$("#add #check").click(function(event){
event.stopPropagation();
});
$("#add #check").change(function() {
var chkLength = $('input[name="check"]:checkbox').length;
var chkdLength = $('input[name="check"]:checkbox:checked').length;
if (chkLength == chkdLength)
{
$('#select').prop('checked', true);
$('#empty').on('click', function() {
localStorage.clear();
$("#favoritesList").listview('refresh');
});
}
else
{
$('#select').prop('checked', false);
}
$("#delete").on('click', function() {
var checked = $('#add #check:checkbox:checked');
var url = localStorage.key(checked);
localStorage.removeItem(url);
$("#favoritesList").listview('refresh');
$("#favoritesList").listview('refresh');
});
});
}
先谢谢。
答案 0 :(得分:30)
您使用布尔值作为键吗?它不会起作用。
应该是:
localStorage.removeItem('keyName');
或
delete window.localStorage["keyName"]
答案 1 :(得分:2)
如果您正在考虑数字键,这里似乎有用。
var elems = -1;
var deleteElem;
然后循环遍历本地存储以查找您要查找的任何元素(在本例中为第一个):
$.each( localStorage, function( key,value ){
elems++;
// define which element to delete
if(elems == 0){ deleteElem = key }
});
现在您已获得所需的密钥,只需将其删除:
localStorage.removeItem(deleteElem);