下文裡技巧實現的效果雖然並不新鮮,但通過jQuery的封裝,HTML實現了很大的清潔。清爽簡潔又高效的代碼任何時候都是開發者所醉心追求的終極目標,也許它簡單,但是它能量巨大。一起來看看百度搜索小編推薦給大家的五個非常實用的jQuery技巧。
1.禁用鼠標右鍵
$(document).ready(function() {
$(document).bind("contextmenu", function(e) {
return false;
});
});
當然jquery1.7版本以後bind()函數推薦用on()來代替。
2.讓內容閃爍起來
$.fn.flash = function(color, duration) {
var current = this.css('color');
this.animate( {color: 'rgb(' + color + ')'}, duration / 2);
this.animate( {color: current}, duration / 2);
}
$('#someid').flash('255,0,0', 1000);
3.DOM加載完成的簡寫形式
$(function() {
// document is ready..
})
4.探測浏覽器
// Safari
if( $.browser.safari )
{
//do something
}
//Above IE6
if ($.browser.msie && $.browser.version > 6 )
{
//do something
}
// IE6 and below
if ($.browser.msie && $.browser.version < 6 ) { //do something } // Firefox 2 and above if ($.browser.mozilla && $.browser.version >= "1.8" )
{
//do something
}
5.判斷元素是否存在
if($("#someDiv").length) {
// yes it does, do something...
}
以上跟大家分享了五個有用的jquery小技巧,希望大家喜歡。