نوشته شده توسط
bitcob589
اگر امکان دارد با C# بنویسید
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie user_selected_theme = Request.Cookies["user_selected_theme"];
if (user_selected_theme != null)
{
HtmlGenericControl css = new HtmlGenericControl();
css.TagName = "link";
css.Attributes.Add("type", "text/css");
css.Attributes.Add("rel", "stylesheet");
css.Attributes.Add("href", "themes/" + user_selected_theme.Value + ".css");
css_theme.Controls.Add(css);
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<asp:PlaceHolder ID="css_theme" runat="server"></asp:PlaceHolder>
<title></title>
<script type="text/javascript" src="jquery.1.7.2.js"></script>
<script type="text/javascript">
var themes = ['black', 'blue', 'red'];
$(document).ready(function () {
$('.theme_switch').click(function () {
disable_themes();
switch_theme($(this).attr('id'));
});
});
function disable_themes() {
for (var i = 0; i < themes.length; i++) {
var css = $('#theme_' + themes[i]);
if (css.length != 0) {
$(css).prop('disabled', true);
}
}
}
function switch_theme(theme) {
var theme_css = $('#theme_' + theme);
if (theme_css.length != 0) {
$(theme_css).removeAttr('disabled');
}
else {
theme_css = $('<link rel="stylesheet" type="text/css">')
.attr('href', 'themes/'+ theme + '.css')
.attr('id', 'theme_' + theme);
$('head').append(theme_css);
}
set_cookie('user_selected_theme', theme, 365);
}
function set_cookie(cookie_name, cookie_value, lifespan_in_days, valid_domain) {
// http://www.thesitewizard.com/javascripts/cookies.shtml
var domain_string = valid_domain ?
("; domain=" + valid_domain) : '';
document.cookie = cookie_name +
"=" + encodeURIComponent(cookie_value) +
"; max-age=" + 60 * 60 *
24 * lifespan_in_days +
"; path=/" + domain_string;
}
function get_cookie(cookie_name) {
// http://www.thesitewizard.com/javascripts/cookies.shtml
var cookie_string = document.cookie;
if (cookie_string.length != 0) {
var cookie_value = cookie_string.match(
'(^|;)[\s]*' +
cookie_name +
'=([^;]*)');
return decodeURIComponent(cookie_value[2]);
}
return '';
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<a class="theme_switch" id="black">Black</a>
<a class="theme_switch" id="blue">Blue</a>
<a class="theme_switch" id="red">red</a>
</div>
</form>
</body>
</html>