body 负载上的主题转换器无法使用单选按钮状态选择和加载选择?
Theme changer on body load is not working using radio button state selection and onload selected?
我已经使用我想要的简单单选按钮创建了一个主题转换器,当单选按钮状态被选中时,该主题应该被选中并工作。
下面是代码
用于本地存储选择。
$(function(){
$('input[type=radio]').each(function(){
var state = JSON.parse( localStorage.getItem('radio_' + $(this).attr('value')) );
if (state) this.checked = state.checked;
}); }); $(window).bind('unload', function(){
$('input[type=radio]').each(function(){
localStorage.setItem('radio_' + $(this).attr('value'), JSON.stringify({checked: this.checked})
);
});
});
*单选按钮代码*
$("input[name$='sidebarccolor']").click(function () {
var radio_theme = $(this).val();
if (radio_theme == 'default') {
$('link[href="css/style.css"]').attr('href','css/style.css');
$('link[href="css/stylered.css"]').attr('href','css/style.css');
$('link[href="css/stylegreen.css"]').attr('href','css/style.css');
$('link[href="css/styleblue.css"]').attr('href','css/style.css');
}
else if (radio_theme == 'red') {
$('link[href="css/style.css"]').attr('href','css/stylered.css');
$('link[href="css/stylered.css"]').attr('href','css/stylered.css');
$('link[href="css/stylegreen.css"]').attr('href','css/stylered.css');
$('link[href="css/styleblue.css"]').attr('href','css/stylered.css');
}
else if (radio_theme == 'green') {
$('link[href="css/style.css"]').attr('href','css/stylegreen.css');
$('link[href="css/stylered.css"]').attr('href','css/stylegreen.css');
$('link[href="css/stylegreen.css"]').attr('href','css/stylegreen.css');
$('link[href="css/styleblue.css"]').attr('href','css/stylegreen.css');
}
else if (radio_theme == 'blue') {
$('link[href="css/style.css"]').attr('href','css/styleblue.css');
$('link[href="css/stylered.css"]').attr('href','css/styleblue.css');
$('link[href="css/stylegreen.css"]').attr('href','css/styleblue.css');
$('link[href="css/styleblue.css"]').attr('href','css/styleblue.css');
} });
选定状态已经生效(这意味着单选按钮选择在 body onload 上运行良好)。但我无法在 body 加载时获得选定的广播主题。
这是你的答案
//choose themes color changer
$("input[name$='sidebarccolor']").click(function () {
var radio_theme = $(this).val();
if (radio_theme == 'default') {
$('link[href="css/style.css"]').attr('href','css/style.css');
$('link[href="css/stylered.css"]').attr('href','css/style.css');
$('link[href="css/stylegreen.css"]').attr('href','css/style.css');
$('link[href="css/styleblue.css"]').attr('href','css/style.css');
}
else if (radio_theme == 'red') {
$('link[href="css/style.css"]').attr('href','css/stylered.css');
$('link[href="css/stylered.css"]').attr('href','css/stylered.css');
$('link[href="css/stylegreen.css"]').attr('href','css/stylered.css');
$('link[href="css/styleblue.css"]').attr('href','css/stylered.css');
}
else if (radio_theme == 'green') {
$('link[href="css/style.css"]').attr('href','css/stylegreen.css');
$('link[href="css/stylered.css"]').attr('href','css/stylegreen.css');
$('link[href="css/stylegreen.css"]').attr('href','css/stylegreen.css');
$('link[href="css/styleblue.css"]').attr('href','css/stylegreen.css');
}
else if (radio_theme == 'blue') {
$('link[href="css/style.css"]').attr('href','css/styleblue.css');
$('link[href="css/stylered.css"]').attr('href','css/styleblue.css');
$('link[href="css/stylegreen.css"]').attr('href','css/styleblue.css');
$('link[href="css/styleblue.css"]').attr('href','css/styleblue.css');
} });
//used for local storage.
$(function(){
$('input[type=radio]').each(function(){
var state = JSON.parse( localStorage.getItem('radio_' + $(this).attr('value')) );
if (state) this.checked = state.checked;
});
});
$(window).bind('unload', function(){
$('input[type=radio]').each(function(){
localStorage.setItem('radio_' + $(this).attr('value'), JSON.stringify({checked: this.checked}));
//$(this).trigger("click");
});
});
setTimeout(function(){
$('input[type=radio]:checked').removeProp("checked");
$('input[type=radio]:checked').attr("checked", true).trigger("click");
});
我已经使用我想要的简单单选按钮创建了一个主题转换器,当单选按钮状态被选中时,该主题应该被选中并工作。
下面是代码
用于本地存储选择。
$(function(){
$('input[type=radio]').each(function(){
var state = JSON.parse( localStorage.getItem('radio_' + $(this).attr('value')) );
if (state) this.checked = state.checked;
}); }); $(window).bind('unload', function(){
$('input[type=radio]').each(function(){
localStorage.setItem('radio_' + $(this).attr('value'), JSON.stringify({checked: this.checked})
);
});
});
*单选按钮代码*
$("input[name$='sidebarccolor']").click(function () {
var radio_theme = $(this).val();
if (radio_theme == 'default') {
$('link[href="css/style.css"]').attr('href','css/style.css');
$('link[href="css/stylered.css"]').attr('href','css/style.css');
$('link[href="css/stylegreen.css"]').attr('href','css/style.css');
$('link[href="css/styleblue.css"]').attr('href','css/style.css');
}
else if (radio_theme == 'red') {
$('link[href="css/style.css"]').attr('href','css/stylered.css');
$('link[href="css/stylered.css"]').attr('href','css/stylered.css');
$('link[href="css/stylegreen.css"]').attr('href','css/stylered.css');
$('link[href="css/styleblue.css"]').attr('href','css/stylered.css');
}
else if (radio_theme == 'green') {
$('link[href="css/style.css"]').attr('href','css/stylegreen.css');
$('link[href="css/stylered.css"]').attr('href','css/stylegreen.css');
$('link[href="css/stylegreen.css"]').attr('href','css/stylegreen.css');
$('link[href="css/styleblue.css"]').attr('href','css/stylegreen.css');
}
else if (radio_theme == 'blue') {
$('link[href="css/style.css"]').attr('href','css/styleblue.css');
$('link[href="css/stylered.css"]').attr('href','css/styleblue.css');
$('link[href="css/stylegreen.css"]').attr('href','css/styleblue.css');
$('link[href="css/styleblue.css"]').attr('href','css/styleblue.css');
} });
选定状态已经生效(这意味着单选按钮选择在 body onload 上运行良好)。但我无法在 body 加载时获得选定的广播主题。
这是你的答案
//choose themes color changer
$("input[name$='sidebarccolor']").click(function () {
var radio_theme = $(this).val();
if (radio_theme == 'default') {
$('link[href="css/style.css"]').attr('href','css/style.css');
$('link[href="css/stylered.css"]').attr('href','css/style.css');
$('link[href="css/stylegreen.css"]').attr('href','css/style.css');
$('link[href="css/styleblue.css"]').attr('href','css/style.css');
}
else if (radio_theme == 'red') {
$('link[href="css/style.css"]').attr('href','css/stylered.css');
$('link[href="css/stylered.css"]').attr('href','css/stylered.css');
$('link[href="css/stylegreen.css"]').attr('href','css/stylered.css');
$('link[href="css/styleblue.css"]').attr('href','css/stylered.css');
}
else if (radio_theme == 'green') {
$('link[href="css/style.css"]').attr('href','css/stylegreen.css');
$('link[href="css/stylered.css"]').attr('href','css/stylegreen.css');
$('link[href="css/stylegreen.css"]').attr('href','css/stylegreen.css');
$('link[href="css/styleblue.css"]').attr('href','css/stylegreen.css');
}
else if (radio_theme == 'blue') {
$('link[href="css/style.css"]').attr('href','css/styleblue.css');
$('link[href="css/stylered.css"]').attr('href','css/styleblue.css');
$('link[href="css/stylegreen.css"]').attr('href','css/styleblue.css');
$('link[href="css/styleblue.css"]').attr('href','css/styleblue.css');
} });
//used for local storage.
$(function(){
$('input[type=radio]').each(function(){
var state = JSON.parse( localStorage.getItem('radio_' + $(this).attr('value')) );
if (state) this.checked = state.checked;
});
});
$(window).bind('unload', function(){
$('input[type=radio]').each(function(){
localStorage.setItem('radio_' + $(this).attr('value'), JSON.stringify({checked: this.checked}));
//$(this).trigger("click");
});
});
setTimeout(function(){
$('input[type=radio]:checked').removeProp("checked");
$('input[type=radio]:checked').attr("checked", true).trigger("click");
});