您可以查看使用浏览器的
Window.sessionStorage
或 Window.localStorage
来存储和检索使用后退按钮/前进按钮的情况下的状态
<script>
window.addEventListener("load", function(event) {
setHTML();
})
function setHTML() {
document.getElementById('change').innerHTML = window.sessionStorage.getItem("state")||"NOT IN FAVORITES";
}
function addToFavorites() {
window.sessionStorage.setItem("state", "Done");
document.getElementById('change').innerHTML = window.sessionStorage.getItem("state");
}
</script>
<div id="change" onclick="addToFavorites();">Change me!</div>
您可以使用 event.preventDefault()
<script>
$(document).ready(function () {
$("button").click(function () {
$("#test").hide();
**event.preventDefault();** //add this line into your js.
});
});
</script>
<div>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p id="test">This is another paragraph.</p>
<button>Click me</button>
</div>