作者:唐小新
原文地址:http://www.cnblogs.com/txiaoxin/p/4928320.html
1.導(dǎo)航菜單背景切換效果
--
在項目的前端頁面里谓传,相對于其它的導(dǎo)航菜單蜈项,激活的導(dǎo)航菜單需要設(shè)置不同的背景。這種效果實現(xiàn)的方式有很多種续挟,下面是使用JQuery實現(xiàn)的一種方式:
<ul id='nav'>
<li>導(dǎo)航一</li>
<li>導(dǎo)航二</li>
<li>導(dǎo)航三</li>
</ul>
//注意:代碼需要修飾完善
$('#nav').click(function(e) {
// 要知道siblings的使用
$(e.target).addClass('tclass').siblings('.tclass').removeClass('tclass');;
});
2.反序訪問JQuery對象里的元素
在某些場景下紧卒,我們可能需要反序訪問通過JQuery選擇器獲取到的頁面元素對象,這個怎么實現(xiàn)呢诗祸?看下面代碼:
//要掌握JQuery對象的get方法 以及數(shù)組的reverse方法即可
var arr = $('#nav').find('li').get().reverse();
$.each(arr,function(index,ele){
.... ...
});
3.訪問IFrame里的元素
在大多數(shù)情況下跑芳,IFrame
并不是好的解決方案轴总,但由于各種原因,項目中確實用到了IFrame
,所以你需要知道怎么去訪問IFrame
里的元素博个。
var iFrameDOM = $("iframe#someID").contents();
//然后怀樟,就可以通過find方法來遍歷獲取iFrame中的元素了
iFrameDOM.find(".message").slideUp();
4.管理搜索框的值
現(xiàn)在各大網(wǎng)站都有搜索框,而搜索框通常都有默認值盆佣,當(dāng)輸入框獲取焦點時往堡,默認值消失。而一旦輸入框失去焦點共耍,而輸入框里又沒有輸入新的值虑灰,輸入框里的值又會恢復(fù)成默認值,如果往輸入框里輸入了新值痹兜,則輸入框的值為新輸入的值穆咐。這種特效用JQuery很容易實現(xiàn):
$("#searchbox")
.focus(function(){$(this).val('')})
.blur(function(){
var $this = $(this);
// '請搜索...'為搜索框默認值
($this.val() === '')? $this.val('請搜索...') : null;
});
5.部分頁面加載更新
為了提高web性能,有更新時我們通常不會加載整個頁面佃蚜,而只是僅僅更新部分頁面內(nèi)容庸娱,如圖片的延遲加載等。頁面部分刷新的特效在JQuery中也很容易實現(xiàn):
setInterval(function() { //每隔5秒鐘刷新頁面內(nèi)容
//獲取的內(nèi)容將增加到 id為content的元素后
$("#content").load(url);
}, 5000);
6.采用data方法來緩存數(shù)據(jù)
在項目中谐算,為了避免多次重復(fù)的向服務(wù)器請求數(shù)據(jù)熟尉,通常會將獲取的數(shù)據(jù)緩存起來以便后續(xù)使用。通過JQuery可以很優(yōu)雅的實現(xiàn)該功能:
var cache = {};
$.data(cache,'key','value'); //緩存數(shù)據(jù)
//獲取數(shù)據(jù)
$.data(cache,'key');
7.采配置JQuery與其它庫的兼容性
如果在項目中使用JQuery洲脂,$
是最常用的變量名斤儿,但JQuery并不是唯一一個使用$
作為變量名的庫,為了避免命名沖突恐锦,你可以按照下面方式來組織你的代碼:
//方法一: 為JQuery重新命名為 $j
var $j = jQuery.noConflict();
$j('#id')....
//方法二: 推薦使用的方式
(function($){
$(document).ready(function(){
//這兒往果,你可以正常的使用JQuery語法
});
})(jQuery);
8.克隆table header到表格的最下面
為了讓table
具有更好的可讀性,我們可以將表格的header
信息克隆一份到表格的底部一铅,這種特效通過JQuery就很容易實現(xiàn):
var $tfoot = $('<tfoot></tfoot>');
$($('thead').clone(true, true).children().get().reverse()).each(function(){
$tfoot.append($(this));
});
$tfoot.insertAfter('table thead');
9.根據(jù)視窗(viewport)創(chuàng)建一個全屏寬度和高度(width/height)的div
下面代碼完全可以讓你根據(jù)viewport
創(chuàng)建一個全屏的div
陕贮。這對在不同窗口大小下展示modal
或?qū)υ捒驎r非常有效:
$('#content').css({
'width': $(window).width(),
'height': $(window).height(),
});
// make sure div stays full width/height on resize
$(window).resize(function(){
var $w = $(window);
$('#content').css({
'width': $w.width(),
'height': $w.height(),
});
});
10.測試密碼的強度
在某些網(wǎng)站注冊時常常會要求設(shè)置密碼,網(wǎng)站也會根據(jù)輸入密碼的字符特點給出相應(yīng)的提示潘飘,如密碼過短肮之、強度差、強度中等卜录、強度強等戈擒。這又是怎么實現(xiàn)的呢?看下面代碼:
<input type="password" name="pass" id="pass" />
<span id="passstrength"></span>
//下面的正則表達式建議各位收藏哦艰毒,項目上有可能會用得著
$('#pass').keyup(function(e) {
//密碼為八位及以上并且字母數(shù)字特殊字符三項都包括
var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
//密碼為七位及以上并且字母筐高、數(shù)字、特殊字符三項中有兩項,強度是中等
var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
var enoughRegex = new RegExp("(?=.{6,}).*", "g");
if (false == enoughRegex.test($(this).val())) {
$('#passstrength').html('More Characters');
} else if (strongRegex.test($(this).val())) {
$('#passstrength').className = 'ok';
$('#passstrength').html('Strong!');
} else if (mediumRegex.test($(this).val())) {
$('#passstrength').className = 'alert';
$('#passstrength').html('Medium!');
} else {
$('#passstrength').className = 'error';
$('#passstrength').html('Weak!');
}
return true;
});
11.使用JQuery重繪圖片的大小
關(guān)于圖片大小的重繪柑土,你可以在服務(wù)端來實現(xiàn)蜀肘,也可以通過JQuery在客戶端實現(xiàn)。
$(window).bind("load", function() {
// IMAGE RESIZE
$('#product_cat_list img').each(function() {
var maxWidth = 120;
var maxHeight = 120;
var ratio = 0;
var width = $(this).width();
var height = $(this).height();
if(width > maxWidth){
ratio = maxWidth / width;
$(this).css("width", maxWidth);
$(this).css("height", height * ratio);
height = height * ratio;
}
var width = $(this).width();
var height = $(this).height();
if(height > maxHeight){
ratio = maxHeight / height;
$(this).css("height", maxHeight);
$(this).css("width", width * ratio);
width = width * ratio;
}
});
//$("#contentpage img").show();
// IMAGE RESIZE
});
12.滾動時動態(tài)加載頁面內(nèi)容
有些網(wǎng)站的網(wǎng)頁內(nèi)容不是一次性加載完畢的稽屏,而是在鼠標(biāo)向下滾動時動態(tài)加載的幌缝,這是怎么做到的呢?看下面代碼:
var loading = false;
$(window).scroll(function(){
if((($(window).scrollTop()+$(window).height())+250)>=$(document).height()){
if(loading == false){
loading = true;
$('#loadingbar').css("display","block");
$.get("load.php?start="+$('#loaded_max').val(), function(loaded){
$('body').append(loaded);
$('#loaded_max').val(parseInt($('#loaded_max').val())+50);
$('#loadingbar').css("display","none");
loading = false;
});
}
}
});
$(document).ready(function() {
$('#loaded_max').val(50);
});
參考文章
http://www.catswhocode.com/blog/10-handy-and-reusable-jquery-code-snippets
http://www.problogdesign.com/coding/30-pro-jquery-tips-tricks-and-strategies/