2016-05-19 6 views
2

私はではありません私は前に入力したものを覚えておくことができるように、ページをリロードした後にボタンのプレスにコピーされているテキストを保存できません。LocalStorageエラー

マイコード:

<html> 
    <body> 
     <input type="text" id="txt"> 
     <input type="button" onclick="myFunction()"> 
     <p id="p"></p> 
     <script> 
      function myFunction() { 
      document.getElementById('p').innerHTML = document.getElementById('txt').value; 

      if (typeof(Storage) !== "undefined") { 
       localStorage.setItem("myinputvalue", document.getElementById("p")[0].innerHTML); 
       localStorage.getItem("myinputvalue"); 
      } 
      } 
     </script> 
    </body> 
</html> 

答えて

3

それは、document.getElementById("p").innerHTMLなければなりません。 document.getElementByIdNODE LISTを返しません。したがって、[0]の必要はありません。

function myFunction() { 
    document.getElementById('p').innerHTML = document.getElementById('txt').value; 

    if (typeof(Storage) !== "undefined") { 
     localStorage.setItem("myinputvalue", document.getElementById("p").innerHTML); 
     console.log(localStorage.getItem("myinputvalue")) 
    } 
} 

これを確認してください。JSFiddle