页面刷新后保持相同的颜色 [英] Keep the same colour after page refresh

查看:142
本文介绍了页面刷新后保持相同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击 div 时, span 颜色 $ c>更改为 red ,但我刷新页面时,颜色不是 red ,它会返回原始颜色,在我的情况下绿色。如何在刷新页面后保留相同的颜色(红色)?
I event尝试了 .apend()但情况是一样的。

When I click the div, the color of the span changes to red, as i expected, but when I refresh my page the color isn't red, it returns back to original color, in my case green. How can I preserve the same color (red) after refreshing the page? I event tried with .apend() but the situation is the same.

HTML:

  <span>Green</span>
  <div id="changeColor"> </div>

jQuery:

$('#changeColor').click(function(){

$('span').css({"background-color":"red"});

});

谢谢大家:D

推荐答案

查看此 jQuery Cookie插件。您可以使用它(或类似的插件)在Cookie中创建/读取和存储信息。然后,当页面加载时,您可以使用此Cookie设置 div 的颜色,因为它存储在cookie中。

Take a look at this jQuery cookie plugin. You can use it (or a smilar plugin) to create / read and store information in cookies. Then when the page loads you can use this cookie to set the color of the div as it is stored in the cookie.

以下是示例 JSFiddle。

Here is an example JSFiddle.

我希望这有助于!

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

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