onclick从不执行功能

时间:2016-09-27 07:22:55

标签: javascript html button

点击“清除存储空间”按钮后,我有以下html代码清除网络存储空间数据。

单击清除存储按钮后,资源管理器(chrome和firefox)永远不会触发该功能。

代码是:

<html>
<head>
<script>

function clear() {
    console.log();
    localStorage.clear();
    refreshContents();
    alert("all cache data cleared!");
}

function refreshContents() {
var str = "";

alert("in refresh");
for (var i = 0, len = localStorage.length; i < len; i++) {
    var k = localStorage.key(i);
    var v = localStorage.getItem(k);
   str += "'" + k + "' = '" + v + "'<br />";
 }
alert("after for loop");
key.value = "";
value.value = "";
content.innerHTML = str;
}
</script>
</head>
<body>
<div class="content"> <!-- end .content -->
<p>This is the logout page.</p>
<p>All temporary data will be erased after logout.    
<p>&nbsp;</p>
<input type="button" onclick="clear();" value="Clear Storage" />&nbsp;
Contents of Local Storage:<br />
<span id="content"></span>
</body>
</html>

有人可以帮忙吗。 提前谢谢!

2 个答案:

答案 0 :(得分:0)

更改功能名称就行了。只需将您的函数名称从clear更改为任何说clearABC,它就会起作用。

function clearABC()
{
}

这篇文章的原因是:is clear a reserved word in javascript

希望这有帮助, 干杯

快乐的编码!!

答案 1 :(得分:-2)

<script type="text/javascript"> 
function clear() {
    console.log();
    localStorage.clear();
    refreshContents();
    alert("all cache data cleared!");
}

function refreshContents() {
var str = "";

alert("in refresh");
for (var i = 0, len = localStorage.length; i < len; i++) {
    var k = localStorage.key(i);
    var v = localStorage.getItem(k);
   str += "'" + k + "' = '" + v + "'<br />";
 }
alert("after for loop");
key.value = "";
value.value = "";
content.innerHTML = str;
}
</script>

更新脚本代码

我认为这个可以帮助你