我有一个按钮#first
,它被点击替换为#second
。获得第二个按钮后,如果我刷新页面,我将回到第一个按钮。如果我想让我的按钮成为刷新后的第二个按钮,我该如何实现这一点? 预先感谢如何防止按钮在刷新时变为原始状态
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("button").click(function() {
$("#first").replaceWith('<button id="second">Second Button</button>');
});
});
</script>
</head>
<body>
<button id="first">firstbutton</button>
</body>
</html>
使用本地存储,并在窗口加载只是从那里得到按钮状态 – thatOneGuy
你需要坚持按钮的状态存储在页面的外部。 sessionStorage,localStorage,cookies或通过AJAX的服务器都可以工作,具体取决于你需要和可用的东西。 –