分享五个有用的jquery小技巧
下文里技巧实现的效果虽然并不新鲜,但通过jQuery的封装,HTML实现了很大的清洁。清爽简洁又高效的代码任何时候都是开发者所醉心追求的终极目标,也许它简单,但是它能量巨大。一起来看看毛票票小编推荐给大家的五个非常实用的jQuery技巧。
1.禁用鼠标右键
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
returnfalse;
});
});
当然jquery1.7版本以后bind()函数推荐用on()来代替。
2.让内容闪烁起来
$.fn.flash=function(color,duration){
varcurrent=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(){
//documentisready..
})
4.探测浏览器
//Safari
if($.browser.safari)
{
//dosomething
}
//AboveIE6
if($.browser.msie&&$.browser.version>6)
{
//dosomething
}
//IE6andbelow
if($.browser.msie&&$.browser.version<6){//dosomething}//Firefox2andaboveif($.browser.mozilla&&$.browser.version>="1.8")
{
//dosomething
}
5.判断元素是否存在
if($("#someDiv").length){
//yesitdoes,dosomething...
}
以上跟大家分享了五个有用的jquery小技巧,希望大家喜欢。