---
title: "html5本地存储"
author: "giga_Zhang"
date: "2011-12-28T10:43:45Z"
tags: []
categories: ["Web前端"]
slug: "html5"
draft: false
lastmod: "2011-12-28T10:43:45Z"
id: 1328361
url: "https://zjsoft.iteye.com/blog/1328361"
---

直接上例子,自己体会 ：

 

 

 

&lt;html&gt;  
&lt;head&gt;  
&lt;title&gt;HTML5 contenteditable example&lt;/title&gt;

&lt;style type="text/css"&gt;  
#scribble-pad {  
       margin-left:auto;  
       margin-right:auto;  
       height: 475px;  
       width: 475px;  
       background-image:url(https://www.ibm.com/developerworks/mydeveloperworks/blogs/bobleah/resource/stickynote.jpg);  
       background-repeat: no-repeat;  
}

#scribble {  
      white-space: pre-wrap;       /* css-3 \*/  
      white-space: -moz-pre-wrap;  /* Mozilla, since 1999 \*/  
      white-space: -pre-wrap;      /* Opera 4-6 \*/  
      white-space: -o-pre-wrap;    /* Opera 7 \*/  
      word-wrap: break-word;       /* Internet Explorer 5.5+ \*/  
      max-width: 300px;  
      padding: 120px 100px 100px 75px;  
      color: #486891;  
      font-family:  Arial,sans-serif;  
      font-size: 140%;  
      font-style: italic;  
      font-weight:bold;  
      line-height: 1.5em;  
}

.c-link {  
      color: #486891;  
      font-family:  Arial,sans-serif;  
      font-size: 95%;  
      font-weight:bold;  
      text-decoration: none;         
}  
&lt;/style&gt;

&lt;script type="text/javascript"&gt;  
function storeUserScribble(id) {  
  var scribble = document.getElementById('scribble').innerHTML;  
  localStorage.setItem('userScribble',scribble);  
}

function getUserScribble() {  
  if ( localStorage.getItem('userScribble')) {  
    var scribble = localStorage.getItem('userScribble');  
  }  
  else {  
    var scribble = 'You can scribble directly on this sticky... and I will also remember your message the next time you visit my blog!';  
  }  
  document.getElementById('scribble').innerHTML = scribble;  
}

function clearLocal() {  
  clear: localStorage.clear();  
  return false;  
}  
&lt;/script&gt;

&lt;/head&gt;  
&lt;body&gt;

&lt;a class="c-link" href='' onclick='javascript:clearLocal();'&gt;Clear local storage&lt;/a&gt;

&lt;div id="scribble-pad"&gt;&lt;pre id="scribble" contenteditable="true" onkeyup="storeUserScribble(this.id);"&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;script&gt;  
  getUserScribble();  
&lt;/script&gt;

&lt;/body&gt;  
&lt;/html&gt;