JQuery 網站換膚功能實現(xiàn)代碼
更新時間:2009年11月02日 02:12:55 作者:
我第一次看到樣式表切換器是在A List Apart或者Simple Bits,那是兩個設計師最應該去的網站。
從那以后,我找到了很多可以讓訪客通過鼠標點擊某個地方切換樣式表的方法。但最近我要寫一篇如何 使用jQuery編寫簡單代碼實現(xiàn)它的教程。
我將向你們逐步解說整個的過程,不僅僅因為要展示jQuery代碼的簡介,同時也要揭示jQuery庫中的若干高級特性。
首先,代碼
$(document).ready(function() {
$('.styleswitch').click(function()
{
switchStylestyle(this.getAttribute("rel"));
return false;
});
var c = readCookie('style');
if (c) switchStylestyle(c);
});
function switchStylestyle(styleName)
{
$('link[@rel*=style][title]').each(function(i)
{
this.disabled = true;
if (this.getAttribute('title') == styleName) this.disabled = false;
});
createCookie('style', styleName, 365);
}
其他這里沒有提到的部分是你將在后面看到的創(chuàng)建和讀取cookie的函數(shù)。
熟悉的開篇
$(document).ready(function(){ $('.styleswitch').click(function()……告訴jQuery“以最快的速度查找所有包含對象名‘styleswitch'的元素,并在他們被鼠標點擊時執(zhí)行一個函數(shù)”。
看起來不錯。當鼠標點擊預先指定的元素時,switchStylestyle函數(shù)將被調用。從現(xiàn)在開始是重點。
這句話什么意思?
第一次看到這句代碼的時候我的腦子有些卡殼:
$('link[@rel*=style]').each(function(i) {
在互聯(lián)網上搜索了一下后我空手而歸。最后不得不找到了jQuery的作者John Resig,向他咨詢。
他直接給了我一個jQuery網站的頁面地址,里面講解了若干jQuery提供的高級特性(xpath),可以用來查找并操作頁面中的若干元素。
如果你看過這些東西你就能明白上面那句神秘的代碼的含義是告訴jQuery“查找所有帶rel屬性并且屬性值字符串中包含‘style'的link鏈接元素”。
讓我們看看如何編寫包含一個主樣式表,兩個備用樣式表的頁面:
<link rel="stylesheet" type="text/css" href="styles1.css" title="styles1" media="screen" /><link rel="alternate stylesheet" type="text/css" href="styles2.css" title="styles2" media="screen" /><link rel="alternate stylesheet" type="text/css" href="styles3.css" title="styles3" media="screen" />我們可以看到所有樣式表都含有一個包含‘style'字串的rel屬性。
所以結果一目了然,jQuery輕松定位了頁面中的樣式表鏈接。
下一步?
each()函數(shù)將遍歷所有這些樣式表鏈接,并執(zhí)行下一行中的代碼:
this.disabled = true;if (this.getAttribute('title') == styleName) this.disabled = false;“首先禁用所有的樣式表鏈接,然后開啟任何title屬性值與switchStylestyle函數(shù)傳遞過來的字串相同的樣式表”
一把抓啊,不過很有效。
現(xiàn)在我們需要保證的是那些樣式表存在并且有效。
完整代碼和演示
既然 Kelvin Luck已經編寫了這些代碼,我就不在這里重復了。
DEMO
我相信Kelvin的靈感是從 這個網站那里得到的,我們正好可以看看使用其他工具實現(xiàn)這個功能是否要比jQuery更加復雜冗長。
完整的styleswitch.js
/**
* Styleswitch stylesheet switcher built on jQuery
* Under an Attribution, Share Alike License
* By Kelvin Luck ( http://www.kelvinluck.com/ )
**/
(function($)
{
$(document).ready(function() {
$('.styleswitch').click(function()
{
switchStylestyle(this.getAttribute("rel"));
return false;
});
var c = readCookie('style');
if (c) switchStylestyle(c);
});
function switchStylestyle(styleName)
{
$('link[@rel*=style][title]').each(function(i)
{
this.disabled = true;
if (this.getAttribute('title') == styleName) this.disabled = false;
});
createCookie('style', styleName, 365);
}
})(jQuery);
// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name)
{
createCookie(name,"",-1);
}
// /cookie functions
我將向你們逐步解說整個的過程,不僅僅因為要展示jQuery代碼的簡介,同時也要揭示jQuery庫中的若干高級特性。
首先,代碼
復制代碼 代碼如下:
$(document).ready(function() {
$('.styleswitch').click(function()
{
switchStylestyle(this.getAttribute("rel"));
return false;
});
var c = readCookie('style');
if (c) switchStylestyle(c);
});
function switchStylestyle(styleName)
{
$('link[@rel*=style][title]').each(function(i)
{
this.disabled = true;
if (this.getAttribute('title') == styleName) this.disabled = false;
});
createCookie('style', styleName, 365);
}
其他這里沒有提到的部分是你將在后面看到的創(chuàng)建和讀取cookie的函數(shù)。
熟悉的開篇
$(document).ready(function(){ $('.styleswitch').click(function()……告訴jQuery“以最快的速度查找所有包含對象名‘styleswitch'的元素,并在他們被鼠標點擊時執(zhí)行一個函數(shù)”。
看起來不錯。當鼠標點擊預先指定的元素時,switchStylestyle函數(shù)將被調用。從現(xiàn)在開始是重點。
這句話什么意思?
第一次看到這句代碼的時候我的腦子有些卡殼:
$('link[@rel*=style]').each(function(i) {
在互聯(lián)網上搜索了一下后我空手而歸。最后不得不找到了jQuery的作者John Resig,向他咨詢。
他直接給了我一個jQuery網站的頁面地址,里面講解了若干jQuery提供的高級特性(xpath),可以用來查找并操作頁面中的若干元素。
如果你看過這些東西你就能明白上面那句神秘的代碼的含義是告訴jQuery“查找所有帶rel屬性并且屬性值字符串中包含‘style'的link鏈接元素”。
讓我們看看如何編寫包含一個主樣式表,兩個備用樣式表的頁面:
<link rel="stylesheet" type="text/css" href="styles1.css" title="styles1" media="screen" /><link rel="alternate stylesheet" type="text/css" href="styles2.css" title="styles2" media="screen" /><link rel="alternate stylesheet" type="text/css" href="styles3.css" title="styles3" media="screen" />我們可以看到所有樣式表都含有一個包含‘style'字串的rel屬性。
所以結果一目了然,jQuery輕松定位了頁面中的樣式表鏈接。
下一步?
each()函數(shù)將遍歷所有這些樣式表鏈接,并執(zhí)行下一行中的代碼:
this.disabled = true;if (this.getAttribute('title') == styleName) this.disabled = false;“首先禁用所有的樣式表鏈接,然后開啟任何title屬性值與switchStylestyle函數(shù)傳遞過來的字串相同的樣式表”
一把抓啊,不過很有效。
現(xiàn)在我們需要保證的是那些樣式表存在并且有效。
完整代碼和演示
既然 Kelvin Luck已經編寫了這些代碼,我就不在這里重復了。
DEMO
我相信Kelvin的靈感是從 這個網站那里得到的,我們正好可以看看使用其他工具實現(xiàn)這個功能是否要比jQuery更加復雜冗長。
完整的styleswitch.js
復制代碼 代碼如下:
/**
* Styleswitch stylesheet switcher built on jQuery
* Under an Attribution, Share Alike License
* By Kelvin Luck ( http://www.kelvinluck.com/ )
**/
(function($)
{
$(document).ready(function() {
$('.styleswitch').click(function()
{
switchStylestyle(this.getAttribute("rel"));
return false;
});
var c = readCookie('style');
if (c) switchStylestyle(c);
});
function switchStylestyle(styleName)
{
$('link[@rel*=style][title]').each(function(i)
{
this.disabled = true;
if (this.getAttribute('title') == styleName) this.disabled = false;
});
createCookie('style', styleName, 365);
}
})(jQuery);
// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name)
{
createCookie(name,"",-1);
}
// /cookie functions
您可能感興趣的文章:
- jQuery基于cookie實現(xiàn)換膚功能實例
- jQuery實現(xiàn)的網頁換膚效果示例
- jQuery實現(xiàn)簡單的網頁換膚效果示例
- Bootstrap框架結合jQuery仿百度換膚功能實例解析
- 使用jQuery實現(xiàn)Web頁面換膚功能的要點解析
- 基于jQuery實現(xiàn)仿百度首頁換膚背景圖片切換代碼
- jQuery實現(xiàn)給頁面換膚的方法
- 基于jquery ui的alert,confirm方案(支持換膚)
- jquery cookie實現(xiàn)的簡單換膚功能適合小網站
- jQuery之網頁換膚實現(xiàn)代碼
- jQuery結合jQuery.cookie.js插件實現(xiàn)換膚功能示例
相關文章
jQuery form插件之ajaxForm()和ajaxSubmit()的可選參數(shù)項對象
這篇文章主要介紹了jQuery form插件之ajaxForm()和ajaxSubmit()的可選參數(shù)項對象的相關資料,需要的朋友可以參考下2016-01-01jquerymobile checkbox及時刷新才能獲取其準確值
一般登錄的時候 都有個記住用戶名 記住密碼 的兩個checkbox 多選框用jquerymobile 做頁面 ,當勾選checkbox 時總是不能獲取它正確的值2012-04-04Jquery利用mouseenter和mouseleave實現(xiàn)鼠標經過彈出層且可以點擊
這篇文章主要介紹了Jquery利用mouseenter和mouseleave實現(xiàn)鼠標經過彈出層且可以點擊。需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02完美解決jQuery fancybox ie 無法顯示關閉按鈕的問題
下面小編就為大家?guī)硪黄昝澜鉀QjQuery fancybox ie 無法顯示關閉按鈕的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11可輸入文字查找ajax下拉框控件 ComBox的實現(xiàn)方法
下面小編就為大家?guī)硪黄奢斎胛淖植檎襛jax下拉框控件 ComBox的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10