2016-05-19 3 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.getElementById не возвращается NODE 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.