The syntax to write a data item in the current domain's localStorage
object is:
localStorage.setItem('key', 'value');
The syntax for reading is as follows:
let key = localStorage.getItem('key');
In your code you're not following the above syntax, but trying to access to an unknown localStorage.toggled
property.
You should fix your JS code like this:
$(".box").toggleClass(localStorage.getItem("toggled"));
$(document).ready(function () {
$(".trigger").on("click", function () {
let $this = $(this).closest(".item").find(".box");
if (localStorage.getItem("toggled") != "hidden") {
$this.toggleClass("hidden", true);
localStorage.setItem("toggled", "hidden");
} else {
$this.toggleClass("hidden", false);
localStorage.setItem("toggled", "");
}
});
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…