在浏览器中后退和前进时保留 Javascript 更改

问题描述 投票:0回答:2
javascript html dom browser back-button
2个回答
2
投票

您可以查看使用浏览器的

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>

0
投票

您可以使用 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>
 
© www.soinside.com 2019 - 2024. All rights reserved.