问答详情
源自:2-1 Html5的几种存储形式

为什么运行错误

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>html5 localStorage实现图片存储</title>
<script>
var src='images/key.png';
function set(key){
 var img=document.createElement('img');
 img.addEventListener('load',function(){
  var imgCanvas=document.creatElement('canvas');
  imgContext=imgCanvas.getContext('2d');
  //确保canvas元素的大小和图片尺寸一致
  imgCanvas.width=this.width;
  imgCanvas.height=this.height;
  
  //渲染图片到canvas中
  imgContext.drawImage(this,0,0,this.widht,this.height);
  
  //用data url的形式取出  
  var imgAsDataURL=imgCanvas.toDataURL('images/png');
  
  //保存到本地存储中
  try{
   localStorage.setItem(key,imgAsDataURL);
  }
  catch(e){
   console.log('Storage failed:'+e)
  }
 },false);
 img.src=src
}
function get(key){
 var srcStr=localStorage.getItem(key);
 var imgobj=document.createElement('img');
 imgobj.src=srcStr;
 document.body.appendChild(imgobj);
}
</script>
</head>

<body>
</body>
</html>

提问者:meicy 2015-05-03 17:48

个回答

  • Lyong
    2016-03-08 16:29:04

    var imgCanvas=document.creatElement('canvas');

    写错了  

    document.createElement  

  • 看尔乃累觉不爱
    2015-10-18 19:50:01

    咦,正好拿来改一改

  • 勇恒的思念
    2015-10-03 19:22:48

    哥,你应该贴出报的错,不然怎么回答你呀