jquery的Theme和Theme Switcher使用小結(jié)

一、引入jquery主題theme
在head中引用jquery主題的CSS鏈接http://ajax.googleapis.com/ajax/libs/jqueryui/1.7/themes/base/jquery-ui.css還有許多其他不同的主題:base, black-tie, blitzer, cupertino, dark-hive, dot-luv, eggplant, excite-bike, flick, hot-sneaks, humanity, le-frog, mint-choc, overcast, pepper-grinder, redmond, smoothness, south-street, start, sunny, swanky-purse, trontastic, ui-darkness, ui-lightness, vader。只要將上面鏈接中的base替換成主題名即可切換不同的主題。
二、使用jquery主題theme
給想要裝飾的部分加上class,如:<a class="ui-state-default ui-corner-all" href="#">Nothing's gonna change my love for you</a> 表示默認(rèn)的ui, corner-all表示圓滑四個(gè)角。
三、增加hover的效果

這里需要使用jquery的腳本。首先在head中引入jquery庫(kù)
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
1.3表示1.3里面的最新版本,現(xiàn)在是1.3.2。
然后手寫腳本;
$(function(){$('.ui-state-default').hover(function(){$(this).addClass('ui-state-hover');},function(){$(this).removeClass('ui-state-hover');});});
這樣就實(shí)現(xiàn)了鼠標(biāo)移到上方是改變樣式的效果了。
四、使用Theme Switcher在前臺(tái)更換主題
先引入庫(kù)
<script type="text/javascript" src="http://jqueryui.com/themeroller/themeswitchertool/"></script>
,然后可以在頁(yè)面任何地方加入層<div id="switcher">主題切換功能載入中...</div>,我習(xí)慣將這個(gè)switch的wikget做成apDiv層,方便挪動(dòng)合適的位置。最后手寫script激活這個(gè)層:
$('#switcher').themeswitcher();

五、使網(wǎng)頁(yè)記住自己的主題
每次更換主題,jquery都會(huì)寫入cookie的,要使網(wǎng)頁(yè)記住自己的主題,只需要提取這個(gè)cookie出來(lái),并且刷新頁(yè)面的css即可。
把腳本寫出來(lái)
$(function(){if(theme==null) updateCSS("http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css");else updateCSS("http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/"+theme+"/jquery-ui.css");})
function updateCSS(locStr){var cssLink=$('<link href="'+locStr+'"type="text/css" rel="Stylesheet" class="ui-theme"/>');$("head").append(cssLink);if($("link.ui-theme").size()>3){$("link.ui-theme:first").remove();}}
最后頁(yè)面代碼大概是這樣子的:
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
#switcher {
position:absolute;
left: 564px;
top: 20px;
}
</style>
<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">google.load("jquery","1.3.2");google.load("jqueryui","1.7.2");function OnLoad(){$('#switcher').html("");var theme=$.cookie('jquery-ui-theme');$(function(){if(theme==null) updateCSS("http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css");else updateCSS("http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/"+theme+"/jquery-ui.css");});$(function(){$('.ui-state-default').hover(function(){$(this).addClass('ui-state-hover');},function(){$(this).removeClass('ui-state-hover');});});$("#pic2").hide();$('#switcher').themeswitcher();}google.setOnLoadCallback(OnLoad);function updateCSS(locStr){var cssLink=$('<link href="'+locStr+'"type="text/css" rel="Stylesheet" class="ui-theme"/>');$("head").append(cssLink);if($("link.ui-theme").size()>3){$("link.ui-theme:first").remove();}}
</script>
<script type="text/javascript" src="http://jqueryui.com/themeroller/themeswitchertool/"></script>
</script>
<title></title>
</head>
<body>
<div id="switcher">主題切換功能載入中...</div>
<p><a class="ui-state-default ui-corner-all" s gonna change my love for you</a></p>
</body>
</html>
相關(guān)文章
jquery 單擊li防止重復(fù)加載的實(shí)現(xiàn)代碼
li在單擊后調(diào)用ajax加載內(nèi)容然后展開(kāi),li不像button在響應(yīng)了單擊事件后可以設(shè)置為disabled。2010-12-12
讓新消息在網(wǎng)頁(yè)標(biāo)題閃爍提示的jQuery代碼
可能有一些站長(zhǎng)會(huì)注意到這樣的效果,到收到的新消息會(huì)閃爍標(biāo)題提示,那么新消息在網(wǎng)頁(yè)標(biāo)題閃爍顯示的效果如何實(shí)現(xiàn),下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下2013-11-11
jQuery 獲取對(duì)象 根據(jù)屬性、內(nèi)容匹配, 還有表單元素匹配
jQuery 練習(xí) 獲取對(duì)象(3) - 根據(jù)屬性、內(nèi)容匹配, 還有表單元素匹配2010-05-05
jQuery 中國(guó)省市兩級(jí)聯(lián)動(dòng)選擇附圖
有關(guān)中國(guó)省市兩級(jí)聯(lián)動(dòng)選擇的實(shí)現(xiàn)代碼在網(wǎng)上可以找到很多,而本文要為大家介紹的是一個(gè)比較實(shí)用的省市兩級(jí)聯(lián)動(dòng)選擇,感興趣的朋友可以參考下2014-05-05
用JQuery 實(shí)現(xiàn)AJAX加載XML并解析的腳本
用JQuery 實(shí)現(xiàn)AJAX加載XML并解析的腳本2009-07-07
使用JQuery和CSS模擬超鏈接的用戶單擊事件的實(shí)現(xiàn)代碼
使用JQuery和CSS模擬超鏈接的用戶單擊事件的實(shí)現(xiàn)代碼,需要的朋友可以參考下2012-05-05

