1. 兩列等高布局
如果使用了兩個(gè)CSS列,使用此種方式可以是兩列的高度相同。
$(document).ready(function() {
function equalHeight(group) {
tallest = 0;
group.each(function() {
thisHeight = $(this).height();
if(thisHeight > tallest) {
tallest = thisHeight;
}
});
group.height(tallest);
}
/*使用方法*/
$(document).ready(function() {
equalHeight($("div"));
});
});
2. 檢測(cè)瀏覽器
$(document).ready(function() {
// Target Firefox 2 and above
if ($.browser.mozilla && $.browser.version >= "1.8" ){
// do something
}
// Target Safari
if( $.browser.safari ){
// do something
}
// Target Chrome
if( $.browser.chrome){
// do something
}
// Target Camino
if( $.browser.camino){
// do something
}
// Target Opera
if( $.browser.opera){
// do something
}
// Target IE6 and below
if ($.browser.msie && $.browser.version <= 6 ){
// do something
}
// Target anything above IE6
if ($.browser.msie && $.browser.version > 6){
// do something
}
});
3. 返回頂部按鈕
你可以利用 animate 和 scrollTop 來(lái)實(shí)現(xiàn)返回頂部的動(dòng)畫(huà),而不需要使用其他插件。
/*Back to top*/
$('a.top').click(function () {
$(document.body).animate({
scrollTop: 0
}, 800);
return false;
});
/* Create an anchor tag */
<a class="top" href="#">Back to top</a>
4. Fix Broken Images Automatically
$('img').on('error', function () {
$(this).prop('src', 'img/broken.png');
});
5.鼠標(biāo)懸停(hover)切換 class 屬性
$('.btn').hover(function () {
$(this).addClass('hover');
}, function () {
$(this).removeClass('hover');
});
只需要添加必要的CSS代碼即可。
如果想要更簡(jiǎn)潔的代碼,可以使用 toggleClass 方法:
你只需要添加必要的CSS代碼即可。如果你想要更簡(jiǎn)潔的代碼,可以使用 toggleClass 方法:
$('.btn').hover(function () {
$(this).toggleClass('hover');
});
6.阻止鏈接加載
$('a.no-link').click(function (e) {
e.preventDefault();
});
7.驗(yàn)證元素是否為空
$(document).ready(function() {
if ($('#id').html()) {
// do something
}
});
8.隨機(jī)數(shù)
1到10之間的隨機(jī)數(shù)
function rd(){
return Math.floor(Math.random()*10+1);
}
alert(rd());
n和m之間的隨機(jī)數(shù)
function rd(n,m){
var c = m-n+1;
return Math.floor(Math.random() * c + n);
}
var n = 5;
var m = 100;
alert(rd(n,m));
9.排序
根據(jù)數(shù)組中的某一項(xiàng)進(jìn)行判斷排序;
function sortByKey(arr,key) {
return arr.sort(function (a,b) {
var x = a[key];
var y = b[key];
return ((x<y)? -1 : ((x>y) ? 1 : 0));
})
}
10.阻止鏈接加載
$('a.no-link').click(function (e) {
e.preventDefault();
});
11.簡(jiǎn)單的手風(fēng)琴效果
// Close all panels
$('#accordion').find('.content').hide();
// Accordion
$('#accordion').find('.accordion-header').click(function () {
var next = $(this).next();
next.slideToggle('fast');
$('.content').not(next).slideUp('fast');
return false;
});
12.延時(shí)加載功能
$(document).ready(function() {
window.setTimeout(function() {
// do something
}, 1000);
});
13.使元素居屏幕中間位置
$(document).ready(function() {
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) /2+$(window).scrollTop() + "px");
this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
return this;
}
$("#id").center();
});
$.fn.abc()是對(duì)jquery擴(kuò)展了一個(gè)abc方法,后面每一個(gè)jquery實(shí)例都可以引用這個(gè)方法了。如$("#div").abc()
14.與其他Javascript類(lèi)庫(kù)沖突解決方案
$(document).ready(function() {
var $jq = jQuery.noConflict();
$jq('#id').show();
});
noConflict() 方法釋放 jQuery 對(duì) $ 變量的控制;該方法也可用于為 jQuery 變量規(guī)定新的自定義名稱(chēng)。