位置:首頁 > 軟件操作教程 > 編程開發(fā) > HTML > 問題詳情

HTML5 存儲和獲取數(shù)據(jù)

提問人:劉團(tuán)圓發(fā)布時間:2020-11-16

    在Web Storage API中,實(shí)現(xiàn)本地數(shù)據(jù)的存儲和讀取是非常簡單的事情,下面通過實(shí)例展示W(wǎng)eb Storage API的使用方法。在下面的例子中,先往本地存儲中寫入數(shù)據(jù),接著會把這些數(shù)捱在控制臺中打印出來。

【例題】存儲和獲取數(shù)據(jù)

代碼如下:

<!DOCTYPE html>

<htinl>

<head>

<meta charset="UTF-8">

<title>webStorage</title〉

<script>

function writeStorage(){

var username = document.getElementByld("username").value; 

var password = document.getElementByld("password").value;

// 存儲到storage中

localStorage.username = username; 

localStorage.password = password;

}

function updateStorage(){ 

localStorage.username = "李四";

}

//輸出所有的Storage 

function readStorage(){ 

console.log(localStorage);

}

</script〉

</head>

<body>

用戶名:〈input type="text" id="username" /><br/>

密碼:〈input type="text" id="password" /><br/>

<input type="button" value="寫入 webStorage" onclick="writeStorage()" />

<input type="button" value="輸出所有的Storage”onclick="readStorage()">

</body>

</html>

繼續(xù)查找其他問題的答案?

相關(guān)視頻回答
回復(fù)(0)
返回頂部