简体   繁体   English

查询 / Json 如何在本地存储中保存数据和标题名称?

[英]Query / Json How can I save Data and Heading name in my local storage?

  $(document).ready(function () {
                $(":button").click(function () {
                    var btn = $(":button").val();
                    
                    if (btn == 'Favoritisieren')
                    
                   
                  $(":button").css("background-color", "red").prop('value', 'Favoritisiert');
                  
                  
                   var obj = {"Hed.1" : $("h1")}; 
                   var myJSON = JSON.stringify(obj);
                   localStorage.setItem('myJSON'); 
              
                    else
                    $(":button").css("background-color","blue").prop('value','Favoritisieren');
                   
                });
               
         });

hey, I would like to save the filename and H1 value locally when clicking the button.嘿,我想在单击按钮时在本地保存文件名和 H1 值。 These should also delete themselves later when the button is pressed again.当再次按下按钮时,这些也应该在稍后自行删除。 Does anyone maybe have an idea where my error lies?有谁知道我的错误在哪里?

Use localStorage.setItem(name, value) to save a named item, var value = localStorage.getItem(name) to read an item, and localStorage.removeItem(name) to delete an item.使用localStorage.setItem(name, value)保存命名项,使用var value = localStorage.getItem(name)读取项,使用localStorage.removeItem(name)删除项。

Docs: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage文档: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

Your code fixed to save the JSON, and to delete it later.您的代码已修复以保存 JSON,并稍后将其删除。

$(document).ready(function () {
  $("button").click(function () {
    var btn = $("button").text();
    if (btn == 'Favoritisieren') {
      $("button").css(({backgroundColor: "red"}).text('Favoritisiert');
      var obj = {"Hed.1" : $("h1").text()}; 
      var myJSON = JSON.stringify(obj);
      localStorage.setItem('myJSON', myJSON); 
    } else {
      $("button").css({backgroundColor: "blue"}).text('Favoritisieren');
      localStorage.removeItem('myJSON'); 
    }
  });
});

Assuming we have a button and h1 like this:假设我们有一个这样的按钮和 h1:

<button value="Favoritisieren">click me</button>
<h1>Hello World</h1>

You can do it like this:你可以这样做:

$(":button").click(function () {
            var btn = $(this).val();
            if (btn == 'Favoritisieren') {
                $(this).css("background-color", "red").prop('value', 'Favoritisiert');
                localStorage.setItem('Hed.1', $("h1").text());
            } else {
                localStorage.removeItem('Hed.1');
                $(this).css("background-color","blue").prop('value','Favoritisieren');
            }
        });

The syntax to write on localStorage:在 localStorage 上写入的语法:

localStorage.setItem('myCat', 'Tom');

The syntax for reading the localStorage item is as follows:读取 localStorage 项的语法如下:

const cat = localStorage.getItem('myCat');

The syntax for removing the localStorage item is as follows:删除 localStorage 项的语法如下:

localStorage.removeItem('myCat');

The syntax for removing all the localStorage items is as follows:删除所有 localStorage 项的语法如下:

localStorage.clear();

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

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