1
我試圖保存輸入到textarea的電子郵件地址localStorage
。我在加載頁面時看到佔位符「您的Gmail地址」,但是當我輸入一個值並單擊「保存」時,就不會發生任何事情。我沒有看到警報或控制檯驗證文本已保存。我究竟做錯了什麼?如何從textarea保存到localStorage?
<html>
<head><title>Extension Options</title></head>
<body>
<textarea id="user" style="margin-bottom: 4px; width: 250px; height: 20px">
</textarea><br />
<button id="save">Save</button>
<!--<button id="save">Clear</button>-->
<script type="text/javascript">
//check if "user" is in localStorage
if (localStorage["user"])
{
var user = localStorage["user"] ;
document.getElementById("user").value = user ;
alert("you are already a user")
}
else
{
document.getElementById("user").placeholder = "your gmail address" ;
console.log("user no found in localStorage")
}
//save entered gmail address
document.getElementById("save").addEventListener
{
"click", function()
{
var user = document.getElementById("user").value ;
//localStorage["user"] = user ;
localStorage.setItem("user", user) ;
alert("gmail id saved") ;
console.log("gmail id saved")
} , false
}
</script>
</body>
</html>
你絕對應該在控制檯中看到一個語法錯誤。 – pimvdb
@ pimvdb,是的,你是對的,我用花括號再次嘗試過,並在控制檯中看到錯誤消息。 – Zeynel