我搜索了有关如何使用 jQuery 设置 cookie(使用 jQuery cookie 插件)的教程,它们似乎都面向比我更有经验的人。
这是一些示例代码:
<button>Example</button>
<div id="whatever" style="background:red;">Test</div>
<script>
$('button').click(function() {
$('#whatever').css("background","yellow");
});
</script>
如何通过 cookie 将 #whatever
的背景保持为黄色?
最佳答案
嗯,这很简单:
//on document ready, checks if the cookie is set, and if so, sets the background with it's value
$(function(){
if($.cookie("background") != null){
$('#whatever').css("background", $.cookie("background"));
}
});
//here you set the cookie, with the value you want
$('button').click(function() {
$('#whatever').css("background","yellow");
$.cookie("background", "yellow");
});
关于javascript - 使用 jQuery 设置 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9057526/