var myFunction = function() {
if (typeof(Storage) !== "undefined") {
var e = this.value;
var current_value = localStorage.getItem("check");
if (current_value == "null") {
current_value = e;
} else {
current_value = current_value + "\n" + e;
}
localStorage.setItem("check", current_value);
console.log(localStorage.getItem("check"));
} else {
console.log('Cannot access local storage.');
}
};
var classname = document.getElementsByClassName("click_button")
for (var i = 0; i < classname.length; i++) {
classname[i].addEventListener("click", myFunction);
}
function clickCount() {
if (typeof(Storage) !== "undefined") {
alert(localStorage.getItem("check"));
} else {
alert("Cannot access local Storage");
}
}
<button type="button" id="first_button" class="click_button">first_button</button>
<button type="button" id="second_button" class="click_button">second_button</button>
<button type="button" id="third_button" class="click_button">third_button</button>
<button onclick="clickCount()" type="button" id="history_button">History_button</button>
当我点击history_button按钮,我不得不提醒被点击,最新的三个按钮(中first_button,second_button和third_button)。这必须使用HTML5 localStorage API来完成。在我做的解决方案中(Iam不是一个很好的编码器,所以我的代码可能是错误的),alert会显示所有以前的点击,而不是最后三次点击。 i。请帮助。
我昨天看到同样的问题还有,它已被删除。它成了非常受欢迎的问题。似乎 – Ravi