LocalStorage错误

时间:2016-05-19 04:39:35

标签: javascript html html5

能够在重新加载页面后保存正在复制按钮的文本,以便我能记住我之前输入的内容。

我的代码:

<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>

1 个答案:

答案 0 :(得分:3)

必须是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")) } }

$JBOSS_HOME/modules/system/layers/base/javax/validation/api/main

选中JSFiddle