我试图让 2 个按钮工作。一个应该保存我的本地存储,另一个应该读取我的本地存储。出于某种原因,他们仍然没有工作。我确实有警报工作,但现在这不起作用。为了读取本地存储,我想使用 jQuery append() 方法。关于我所缺少的任何想法。
脚本
$(document).ready(function() {
function saveLocal(){
if (window.localStorage) {
localStorage.setItem("firstname","myfirstname");
localStorage.setItem("lastname","mylastname");
localStorage.setItem("state","mystate");
alert("The data has been saved locally.");
} else {
alert("Your Browser does not support LocalStorage.");
}
}
function readLocal(){
if (window.localStorage) {
var firstname = localStorage.getItem("myfirstname");
var lastname = localStorage.getItem("mylastname");
var coursetitle = localStorage.getItem("mystate");
$("#message").empty().append( firstname + " " + lastname + " " + state );
}else {
alert("Your Browser does not support LocalStorage.");
}
}
}); // end ready
html
</div>
</p>
<p>
<div id="main">
<input type="button" value="Save Values" onclick="saveLocal()"/>
<input type="button" value="Read Values" onclick="readLocal()"/>
</div>