Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 本地存储错误_Javascript_Html - Fatal编程技术网

Javascript 本地存储错误

Javascript 本地存储错误,javascript,html,Javascript,Html,我无法保存在重新加载页面后按按钮复制的文本,以便记住之前键入的内容 我的代码: <html> <body> <input type="text" id="txt"> <input type="button" onclick="myFunction()"> <p id="p"></p> <script> function myFunction

无法保存在重新加载页面后按按钮复制的文本,以便记住之前键入的内容

我的代码:

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

函数myFunction(){ document.getElementById('p').innerHTML=document.getElementById('txt').value; if(类型(存储)!=“未定义”){ localStorage.setItem(“myinputvalue”,document.getElementById(“p”)[0].innerHTML); getItem(“myinputvalue”); } }
它必须是
document.getElementById(“p”).innerHTML
<代码>文档。getElementById不返回节点列表。因此不需要
[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"))
    }
} 
看看这个