注:引入JS的顺序jquery>cookie>easyuicss>changeEasyUITheme.js
首先将easyui的样式文件加入一个ID,这里命名为easyuiTheme,然后在样式文件下面加入一个JS文件
1 <script type="text/javascript" charset="UTF-8" src="jslib/jquery-easyui-1.2.5/jquery-1.7.1.min.js"></script> 2 3 <script type="text/javascript" charset="UTF-8" src="jslib/jquery.cookie.js"></script> 4 5 <link id="easyuiTheme" rel="stylesheet" type="text/css" href="jslib/jquery-easyui-1.2.5/themes/gray/easyui.css"> 6 <script type="text/javascript" charset="UTF-8" src="jslib/changeEasyuiTheme.js"></script> 7 <link rel="stylesheet" type="text/css" href="jslib/jquery-easyui-1.2.5/themes/icon.css"> 8 <script type="text/javascript" charset="UTF-8" src="jslib/jquery-easyui-1.2.5/jquery.easyui.min.js"></script> 9 <script type="text/javascript" charset="UTF-8" src="jslib/jquery-easyui-1.2.5/locale/easyui-lang-zh_CN.js"></script>
changeEasyuiTheme.js文件的内容是
1 function changeThemeFun(themeName) {/* 更换主题 */ 2 var $easyuiTheme = $('#easyuiTheme'); 3 var url = $easyuiTheme.attr('href'); 4 var href = url.substring(0, url.indexOf('themes')) + 'themes/' + themeName + '/easyui.css'; 5 $easyuiTheme.attr('href', href); 6 7 var $iframe = $('iframe'); 8 if ($iframe.length > 0) { 9 for ( var i = 0; i < $iframe.length; i++) { 10 var ifr = $iframe[i]; 11 $(ifr).contents().find('#easyuiTheme').attr('href', href); 12 } 13 } 14 15 $.cookie('easyuiThemeName', themeName, { 16 expires : 7 17 }); 18 }; 19 if ($.cookie('easyuiThemeName')) { 20 changeThemeFun($.cookie('easyuiThemeName')); 21 }
jquery.cookie.js的内容是
1 jQuery.cookie = function (key, value, options) { 2 3 // key and value given, set cookie... 4 if (arguments.length > 1 && (value === null || typeof value !== "object")) { 5 options = jQuery.extend({}, options); 6 7 if (value === null) { 8 options.expires = -1; 9 } 10 11 if (typeof options.expires === 'number') { 12 var days = options.expires, t = options.expires = new Date(); 13 t.setDate(t.getDate() + days); 14 } 15 16 return (document.cookie = [ 17 encodeURIComponent(key), '=', 18 options.raw ? String(value) : encodeURIComponent(String(value)), 19 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE 20 options.path ? '; path=' + options.path : '', 21 options.domain ? '; domain=' + options.domain : '', 22 options.secure ? '; secure' : '' 23 ].join('')); 24 } 25 26 // key and possibly options given, get cookie... 27 options = value || {}; 28 var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent; 29 return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null; 30 };
使用的时候
changeThemeFun('default');
changeThemeFun('gray');