页面刷新后如何保留div内容 [英] how to retain the div contents after page refresh

查看:104
本文介绍了页面刷新后如何保留div内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jquery创建一个动态div,并且试图对其进行缓存,以便在页面刷新后将其取回,请帮助实现该目标,

I am creating a dynamic div using Jquery and I am trying to cache it so that, I will get it back after the page refresh, please help on how to achieve this,

粘贴下面的代码

<script src="jquery-1.3.2.js" type="text/javascript"></script>
<script src="jQuery.jCache.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
    $.jCache.maxSize = 20;
    function createDiv() {

        var divTemp = "<div><label>This is a Label</Label></div>";
        alert(divTemp);
        $(divTemp).appendTo("#divDisplay");
        $.jCache.setItem("myKey", divTemp);
    }

    $(document).ready(function() {


    if ($.jCache.hasItem("myKey")) {
        var cacheDiv = $.jCache.getItem("myKey");
            var cacheDiv = cacheDiv + "<a>anchor Tag</a>";
            $(cacheDiv).appendTo("#divDisplay");

        }
    }); 
</script>

我正在尝试使用按钮单击在页面中绘制div,在页面中绘制单击div之后,使用F5单击刷新其div后,div消失了,有什么办法可以通过缓存保留div的内容机制?

I am trying to draw a div in the page using button click, after the click div is drawn in the page, when its refreshed using an F5 click, the div disappears, is there any way to retain the div contents using caching mechanisms?

这是我的全部功能的绝招!

this is been a showstopper for my whole functionality!

预先感谢, 肖乔

推荐答案

最好的选择是跟踪div内容服务器端.这样,每次用户更新div的内容时,都可以进行AJAX调用来更新div的服务器端表示.

The best bet would be to keep track of the div content server side. That way everytime the user updates the content of the div, an AJAX call can be made updating the server side representation of the div.

首次加载页面时,您的服务器可以预填充div.然后,看起来好像页面在重新加载后保留了div的内容.

When the page loads for the first time your server can prepopulate the div. It then appears as if the page retains the content of the div after a reload.

这种功能现在已在许多站点中使用,因此我确信它可以在您的情况下使用.查看此站点(SO).当您投票时,实际上是在浏览器上更改投票箭头的颜色.如果在颜色不还原后刷新,它会以某种方式记住您已投票.那是因为当您单击投票箭头时,会进行AJAX调用,并且您已投票的事实会记录在服务器上.

This type of functionality is used in numerous sites now so I'm sure it can work for your scenario. Look at this site (SO). When you vote you are essentially changing something on the browser, the color of the voting arrow. If you refresh after the color does not revert, it somehow remembers that you voted. Thats because when you click the vote arrow an AJAX call is made and the fact that you voted is recorded on the server.

这篇关于页面刷新后如何保留div内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆