昼/夜模式- CSS + JQuery - cookie
Day/Night mode - CSS + JQuery - Cookies?
我正在测试javascript代码白天/灯光背景切换,我不知道如何做某事。我是javascript的新手,所以我在学习新的东西。
我想做什么?
当我点击例如按钮"天"(改变背景为黄色),我希望黄色背景的样式在页面刷新后留在代码中。我听说过cookie/LocalStorage,但我不知道如何为这段代码实现它。
如果你知道更简单的方法,可以随意修改整个代码,但请解释为什么这样更好,或者为什么应该这样。
提前感谢您的帮助。
代码如下:
HTML:<body id="body">
<input type="button" onclick="day();" value="Day" />
<input type="button" onclick="night();" value="Night" />
<input type="button" onclick="reset();" value="Reset" />
</body>
CSS: .darkSwitch {
background: #808080;
}
.lightSwitch {
background: #ffff99;
}
JavaScript: function day() {
body.className = "lightSwitch";
};
function night() {
body.className = "darkSwitch";
};
function reset() {
body.className = "";
};
$(function() {
var button = $('input[type=button]');
button.on('click', function() {
button.not(this).removeAttr('disabled');
$(this).attr('disabled', '');
});
});
最后编辑:现在禁用页面加载中的选定按钮,代码不在本文中,请参阅最新的JSFiddle
<标题>我做了什么:
- 代码放在
<body>
(个人偏好) 末尾的 - 我在
button
元素的onClick
事件中增加了参数event
。 - 我在
button
元素的onclick
事件开始时添加了event.preventDefault()
:确保单击按钮时页面不会刷新。
<script>
标签之间。
警告: 所有按钮将在您的页面中表现相同。如果您有其他按钮,我建议您为这三个按钮添加另一个类,并将事件绑定到button.myClass
元素上。
- 我在
button
状态改变上添加了一个条件,所以reset按钮不会被禁用。 -
eval($(this).val().toLowerCase()+"();");
获取被单击的button
的值,并执行附加在其上的函数。
<标题> 解决方案
HTML
<body id="body">
<input type="button" class="changeBg" onclick="day();" value="Day" />
<input type="button" class="changeBg" onclick="night();" value="Night" />
<input type="button" class="changeBg" onclick="reset();" value="Reset" />
</body>
JavaScript
(JSFiddle) <——查看这个更新了类&饼干
function day() {
body.className = "lightSwitch";
};
function night() {
body.className = "darkSwitch";
};
function reset() {
body.className = "";
};
$(function () {
/* RegEx to grab the "bgColor" cookie */
var bgColor = document.cookie.replace(/(?:(?:^|.*;'s*)bgColor's*'='s*([^;]*).*$)|^.*$/, "$1");
var button = $('input[type=button].changeBg');
button.on('click', function (event) {
event.preventDefault();
/* Executing the function associated with the button */
eval($(this).val().toLowerCase() + "();");
button.not($(this)).removeAttr('disabled');
if ($(this).val() != "Reset") {
$(this).attr('disabled', '');
/* Here we create the cookie and set its value, does not happen if it's Reset which is fired. */
document.cookie = "bgColor="+$(this).val();
}
});
/* If the cookie is not empty on page load, execute the function of the same name */
if(bgColor.length > 0)
{
eval(bgColor.toLowerCase()+'()');
/* Disable the button associated with the function name */
$('button[value="'+bgColor+'"]').attr("disabled","disabled");
}
});
标题>
标题>
除非不支持localStorage,否则我建议您不要使用cookie。它们会减慢你的网站速度。
if(localStorage){
localStorage.setItem("bgColor", "lightSwitch");
}else{
document.cookie = "bgColor=lightSwitch";
}
相关文章:
- 使用jquery cookie的第三方cookie
- Jquery cookie - toggle box
- 如何使用 jquery.cookie 创建 if 语句
- 基于 jQuery Cookie 打开和关闭面板
- jquery cookie,在清除浏览器历史记录后仍然设置
- 为什么jQuery.cookie插件返回“[object object]”
- jQuery可以't使用jquery.cookie插件读取cookie
- jQuery cookie is not expring
- 正在保存CSS State Jquery Cookie
- 如何使用jquery cookie插件保存toggleClass方法的状态
- JQuery Cookie在1分钟后过期
- JQuery Cookie 不起作用
- jQuery Cookie 插件许可证
- jquery cookie 通过循环追加值
- JQuery cookie 在其他函数中设置
- 使用函数setTimerOut和jquery Cookie的模型窗口
- jQuery Cookie插件保存选择值
- jquery cookie for jquery animate
- 为什么这个 JQuery Cookie 值没有更新
- 将对象/数组存储在 jQuery Cookie 中