-1

我有此代码用于将表单数据保存到本地存储中,然后通过替换<span id="recalledtext" >Dear Visitors</span>为 . 内部HTML。

   <HTML>
    <head>
    <script>
    function myfunction1(){texttosave = document.getElementById('textline').value ; localStorage.setItem('mynumber', texttosave); } function myfunction2(){document.getElementById('recalledtext').innerHTML = localStorage.getItem('mynumber'); }
    </script>
    </head>
    <body onload='myfunction2()'>
    <input type="text" id="textline" placeholder="Enter Your Name"/> <button id="rememberer" onclick='myfunction1()'>Save</button>
    <br>
     Welcome<span id="recalledtext" >Dear Visitors</span> Refresh the page to see changes
    </body>
    </HTML>

它工作得很好,但我还想要一个删除数据按钮。所以我把代码改成这样:

<HTML>
<head>
<script>
function myfunction1(){texttosave = document.getElementById('textline').value ; localStorage.setItem('mynumber', texttosave); } function myfunction2(){document.getElementById('recalledtext').innerHTML = localStorage.getItem('mynumber'); } function myfunction3() localStorage.removeItem('mynumber'); return '';}
</script>
</head>
<body onload='myfunction2()'>
<input type="text" id="textline" placeholder="Enter Your Name"/> <button id="rememberer" onclick='myfunction1()'>Save</button> <button id="recaller" onclick='myfunction3()'>Delete Your Name</button>
<br>
 Welcome<span id="recalledtext" >Dear Visitors</span> Refresh the page to see changes
</body>
</HTML>.

但是在添加函数 myfunction3() 之后,代码完全停止工作。

4

1 回答 1

1

我自己找到了答案。我为你服务了很长时间,然后我开始检查 JavaScript 代码。缺少字符“{”这是正确的代码

    <HTML><head>
<script>
function myfunction1(){texttosave = document.getElementById('textline').value ; localStorage.setItem('mynumber', texttosave); } function myfunction2(){document.getElementById('recalledtext').innerHTML = localStorage.getItem('mynumber'); } function myfunction3() localStorage.removeItem('mynumber'); return '';}
</script>
</head>
<body onload='myfunction2()'>
<input type="text" id="textline" placeholder="Enter Your Name"/> <button id="rememberer" onclick='myfunction1()'>remember text</button> <button id="recaller" onclick='myfunction3()'>Delete Your Name</button>
<br>
 Welcome<span id="recalledtext" >Dear Visitors</span> Refresh the page to see changes
</body>
</HTML>
于 2017-08-06T02:27:06.983 回答