2016-09-27 179 views
1

點擊「清除存儲」按鈕後,我有以下html代碼清除網絡存儲數據。onclick永不執行功能

資源管理器(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> 

有人可以幫助請。 在此先感謝!

+0

嘗試將名稱更改爲'clearStorage' – Tushar

回答

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

更新腳本標籤

我覺得這一個幫助你

+0

否。JavaScript是默認類型,從HTML 5開始,type屬性是可選的。當你編寫JavaScript時,它唯一做的事就是讓你有機會犯一個錯字並破壞腳本。 – Quentin

0

更改函數名並獲得成功。只需將您的函數名稱從clear更改爲clearABC即可。

function clearABC() 
{ 
} 

這則訊息的原因:is clear a reserved word in javascript

希望這有助於 乾杯

編碼愉快!