javascript js/html5 显示本地存储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19667308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:24:33  来源:igfitidea点击:

js/html5 Displaying local storage

javascriptjqueryhtmllocal-storage

提问by user1

Is there a simple way to display my local storage with document.writeor document.innerHTMLmethods? It's being stored on 1 page, trying to display on separate. I'm new to js and just unsure how to build the syntax of those methods with how I'm storing it(if possible).

有没有一种简单的方法可以使用document.writedocument.innerHTML方法显示我的本地存储?它被存储在 1 页上,试图单独显示。我是 js 的新手,只是不确定如何使用我的存储方式来构建这些方法的语法(如果可能的话)。

 $('form').submit(function() {
    var person = $("#FirstName").val() + "." + $('#LastName').val();
 $('input, select, textarea').each(function() {
    var value = $(this).val(),
       name = $(this).attr('name');
       localStorage[person + "." + name] = value;
       window.location.href = "Confirmation.html";
    console.log('stored key: '+name+' stored value: '+value);
});   
});

heres the whole if it helps: http://jsfiddle.net/EUWFN/

如果有帮助,这里是整个:http: //jsfiddle.net/EUWFN/

回答by paka

As local storage is Object, you can go trough all it keys and get in values in simple way

由于本地存储是对象,您可以通过它的所有键并以简单的方式获取值

for (var key in localStorage) {
  console.log(key + ':' + localStorage[key]);
}

To print it to the screen you can use something like this:

要将其打印到屏幕上,您可以使用以下内容:

var output = ''; 

for (var key in localStorage) {
  output = output+(key + ':' +localStorage[key])+'\n';
}

$('#DivToPrintOut').html(output);

回答by Dirk Bruins

If you're using Chrome, press F12 for the Developers Tools.

如果您使用的是 Chrome,请为开发人员工具按 F12。

In the Console type localStorageand press enter.

在控制台中键入localStorage并按回车键。

Or in your js code put console.log(localStorage);.

或者在你的 js 代码中放置console.log(localStorage);.

In the console, click on the little triangle next to line that begins with Storage.

在控制台中,单击以 开头的行旁边的小三角形Storage

Or in Dev Tools:

或者在开发工具中:

  1. click on the Resourcestab at the top
  2. click on the little triangle next to Local Storagein the frame on the left
  1. 单击Resources顶部的选项卡
  2. 单击Local Storage左侧框架中旁边的小三角形