使用 HTML5 localstorage 更新 json 键的值

update the value of the json key using HTML5 localstorage

var data = {  
       "mid":"10000XXX",          
       "css":{  
          "header":{  
             "background-color":"#000",
             "color":"#fff",
             "font-size":"10"
          },
          "txndescr":{  
             "background-color":"#000",
             "color":"#fff",
             "font-size":"10"
          }          
       }
    };

我已经通过以下方式存储在localstrorage中,

// Put the object into storage
localStorage.setItem('defaultTheme', JSON.stringify(data));

我想更新 'header' 背景颜色(#000 到 #FFF)属性。如何更新值。

更新localStorage中已有的Object的方法如下:

  1. 获取现有项目
  2. 更新值
  3. 保存更新的项目

    //1. Fetch the existing item
    var data = JSON.parse(localStorage.getItem('defaultTheme'));
    //2. Update the value
    data.css.header['background-color'] = '#CCC';
    //3. Save the update item
    localStorage.setItem('defaultTheme', JSON.stringify(data));
    

干杯