easyui換皮膚本身實現了,可是下次打開瀏覽器的時候,上次選中的皮膚又變回默認皮膚了,怎樣讓瀏覽器記住本身所選的皮膚。這裏我給出個人解決方案。javascript
注:引入JS的順序jquery>cookie>easyuicss>changeEasyUITheme.jscss
首先將easyui的樣式文件加入一個ID,這裏命名爲easyuiTheme,而後在樣式文件下面加入一個JS文件java
<script type="text/javascript" charset="UTF-8" src="jslib/jquery-easyui-1.2.5/jquery-1.7.1.min.js"></script>changeEasyuiTheme.js文件的內容是jquery
function changeThemeFun(themeName) {/* 更換主題 */瀏覽器
var $easyuiTheme = $('#easyuiTheme');cookie
var url = $easyuiTheme.attr('href');dom
var href = url.substring(0, url.indexOf('themes')) + 'themes/' + themeName + '/easyui.css';ui
$easyuiTheme.attr('href', href);url
var $iframe = $('iframe');spa
if ($iframe.length > 0) {
for ( var i = 0; i < $iframe.length; i++) {
var ifr = $iframe[i];
$(ifr).contents().find('#easyuiTheme').attr('href', href);
}
}
$.cookie('easyuiThemeName', themeName, {
expires : 7
});
};
if ($.cookie('easyuiThemeName')) {
changeThemeFun($.cookie('easyuiThemeName'));
}
jquery.cookie.js的內容是
jQuery.cookie = function (key, value, options) {
// key and value given, set cookie...
if (arguments.length > 1 && (value === null || typeof value !== "object")) {
options = jQuery.extend({}, options);
if (value === null) {
options.expires = -1;
}
if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}
return (document.cookie = [
encodeURIComponent(key), '=',
options.raw ? String(value) : encodeURIComponent(String(value)),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}
// key and possibly options given, get cookie...
options = value || {};
var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
使用的時候
changeThemeFun('default');
changeThemeFun('gray');