& lt; select>选项在更改页面后保持不变
<select> option stay same after changing pages
我添加了一个主题选择菜单到我的网站,我想让主题选择保持不变后,你改变页面,因为我添加了选择菜单到每个页面,但它不断重置时改变页面,有人可以告诉我如何做到这一点?我的代码:
<select onchange="javascript:changeColor(this);">
<option selected disabled hidden>Theme</option>
<optgroup label="Themes"></optgroup>
<option value="white">Light</option>
<option value="#222222">Dark</option>
<option value="red">Red</option>
</select>
<script>
function changeColor(el) {
var theme = el.value;
document.body.style.background = theme;
}
</script>
您可以将您想要的颜色存储在localStorage
:
游乐场:http://jsbin.com/cisicay/2/edit?html、css js,控制台输出
// On page load let's see if we stored any theme color
var theme = window.localStorage.theme;
// This function can be both triggered as Standalone
// or on Select onchange event:
function changeColor(el) {
// If it was triggered by our Select element store the new theme color
if(el) theme = window.localStorage.theme = el.value;
// Set the color
if(theme) document.body.style.background = theme;
}
// Trigger theme change on page load
changeColor();
<select onchange="changeColor(this);">
<option disabled hidden>Theme</option>
<optgroup label="Themes">
<option value="#eee">Light</option>
<option value="#222">Dark</option>
<option value="red">Red</option>
</optgroup>
</select>
也改变选择值
这是另一个例子,将改变你的select
值以及页面加载和回退到#eee
(轻变体)
(注意:要使其工作,请删除默认的selected
选项属性!并为SELECT元素添加ID)
游乐场:http://jsbin.com/cisicay/3/edit?html、js、控制台、输出
function changeColor(el) {
document.body.style.background = localStorage.theme = el ? el.value : localStorage.theme || "#eee";
document.getElementById("theme").value = localStorage.theme
}
changeColor();
<select id="theme" onchange="changeColor(this);">
<option disabled hidden>Theme</option>
<optgroup label="Themes">
<option value="#eee">Light</option>
<option value="#222">Dark</option>
<option value="red">Red</option>
</optgroup>
</select>
您的脚本将在每次页面加载时运行。您需要存储选中的值,并在页面加载时检查它。
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function changeColor(el) {
var theme = el.value;
document.body.style.background = theme;
createCookie('siteTheme', theme, 365);
}
$(function() {
var theme = readCookie('siteTheme');
if( theme != '' && theme != 'undefined') {
document.body.style.background = theme;
}
});
<select onchange="javascript:changeColor(this);">
<option selected disabled hidden>Theme</option>
<optgroup label="Themes">
<option value="white">Light</option>
<option value="#222222">Dark</option>
<option value="red">Red</option>
</optgroup>
</select>
相关文章:
- 如何获得strut<html:select>jsp中java脚本中的值
- 处理& lt; select>在node.js/express中的Form参数
- 禁用& lt; select>当复选框启用时
- 导航到URL取决于哪个<选项>在& lt; select>在javascript中选择
- & lt; select>举个例子,最大限制选择5个
- 存储/生成& lt; select>供以后使用的选项
- & lt; select>当表单加载时,标签不加载所选项目
- & lt; select>Getter-setter接收字符串形式的对象参数
- 搜索& lt; select>在之前的多个元素之前
- AngularJS & lt; select>.选项上的' ng-click '不是工作
- & lt; select>选项在更改页面后保持不变
- HTML & lt; select>在页面加载时打开
- 访问& lt; select>onChange在木偶ItemView
- HTML & lt; select>焦点问题:使用Firefox需要2次点击来选择一个<选项>
- 触发& lt; select>另一个元素的click
- & lt; select>下拉输出MYSQL数据正确使用JS函数
- 单& lt; select>有多个选择选项
- 设置& lt; option>& lt; select>元素
- 转换& lt; select>在ASP.NET中将jQuery的选项转换为数组
- 使用jqtransform出错.& # 39;抵消().top& # 39;为空或不是对象.& lt; select>是