繁体   English   中英

JS-如何在OnePage上使用sessionStorage?

[英]JS - How to use on an OnePage the sessionStorage?

我确实有一个OnePage website ,可以在其中添加一些注释到列表中。 通常我使用copy & paste功能

每次在我的网站上单击“ Add to the list按钮时,JavaScript都会生成HTML。 因此,新条目将追加到现有列表中。

我想保存这些条目,并且如果我的网站刷新,它应该仍然可用。 请注意其OnePage网站。

我确实还有一个“删除”按钮,如果不需要,可以在其中删除一些条目。

// save my entries
$(function() {
  sessionStorage["myEntries"] = $('#id_This_Is_The_ID_of_the_NewEntry_That_I_Want_To_Save');
});


// restore my saved entries - IMPORTANT OnLoad
  if (sessionStorage["myEntries"] != null) {
    var SavedContent = sessionStorage["myKey"];
    $("#id_Of_The_Place_Where_The_Saved_HTML_Should_Be_Paste").html(SavedContent);
  } 


// remove entry from saved list
$(document).on('click', '.class_RemoveEntry', function() {
  sessionStorage.removeItem($(this).parents('table').remove());
});

不幸的是,我的代码无法正常工作。 它应该保存它,并且如果我刷新我的网站,它应该再次恢复它。 我怎么了

您想尝试localStorage sessionStorage仅存储单个会话(直到重新加载)。 它的工作方式几乎相同,只是将所有sessionStorage实例替换为localStorage 我建议您查看MDN文档 请记住addItem()getItem()removeItem()

希望对您有所帮助!

暂无
暂无

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

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