简体   繁体   English

当按下后退按钮时,Javascript会更改为丢失的dom

[英]Javascript changes to the dom lost when back button pressed

I have this view that changes the text inside a div. 我有这个视图,改变div内的文本。

The user can then click on a link to jump to another page. 然后,用户可以单击链接以跳转到另一个页面。 But when the user presses the "back" button all DOM changes are lost. 但是当用户按下“后退”按钮时,所有DOM更改都将丢失。

FF remembers the changed div text but Chrome and IE do not. FF会记住更改的div文本,但Chrome和IE不会。

I've found similar question, but in my case it is not possible to summarize the state by a url hash because the div contains random data. 我发现了类似的问题,但在我的情况下,不可能通过url哈希来概括状态,因为div包含随机数据。

I need is so that when the user goes back, the div is filled from the identical series of numbers. 我需要的是当用户返回时,div从相同的数字序列中填充。

<script type="text/javascript">
    function ChangeText() {
        var newText = "";

        for (var i = 0; i < 10; i++) {
            newText = newText + Math.random() * 100;
            newText = newText + "<br />";
        }

        $("#container").html(newText);
    }
</script>

<div id="container">
    INITIAL TEXT
</div>
<button onclick="ChangeText()">Click here to generate random numbers</button>
<br/>
<a href="http://www.google.com">External link</a>

You can store your html in LocalStorage, so when user go back, you fill the content with your localStorage: 您可以将您的html存储在LocalStorage中,因此当用户返回时,您可以使用localStorage填充内容:

<script type="text/javascript">
    function onLoadPage(){
        if(window.localStorage.getItem("newText") != null){
            $("#container").html(window.localStorage.getItem("newText"));
        }
    }

    function ChangeText() {
        var newText = "";

        for (var i = 0; i < 10; i++) {
            newText = newText + Math.random() * 100;
            newText = newText + "<br />";
        }

        window.localStorage.setItem("newText") = newText;

        $("#container").html(newText);
    }
</script>

<div id="container">
    INITIAL TEXT
</div>
<button onclick="ChangeText()">Click here to generate random numbers</button>
<br/>
<a href="http://www.google.com">External link</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在反应应用程序中按下刷新或后退按钮时,CSS 丢失 - CSS gets lost when refresh or back button pressed in react app 使用后退按钮时保留DOM更改 - Preserve DOM changes when back button is used 按下时,按钮会更改文本并在一段时间后返回 - When pressed, the button changes the text and returns back after a while 当按下按钮或div时如何更改背景颜色,如果再按一次则先改变颜色? - How to change background color when button or div pressed and if pressed another one the first one changes color back? 使用 JavaScript 按下浏览器后退按钮时重定向到 URL - Redirect to a URL when browser back button is pressed using JavaScript 单击按钮时更改JavaScript中的CSS样式,但按下另一个按钮时更改回原始样式 - Change CSS style in JavaScript when button is clicked but change back to original when another button is pressed 当按下浏览器后退按钮(在 Javascript 中)时,如何将特定变量传递给 go 返回上一页? - How to pass a specific variable to go back to the previous page when the browser back button is pressed (In Javascript)? 如果其他情况下按下按钮时 - If Else for when a button is pressed javascript 按下后退按钮时检测/警告 - Detect/Warn when back button is pressed 按下后退按钮时完成所有活动 - Finish all activities when back button is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM