zl程序教程

您现在的位置是:首页 >  前端

当前栏目

Jquery常用技巧收集整理篇

jQuery 技巧 常用 收集整理
2023-06-13 09:14:25 时间
比如有禁止右键点击、隐藏搜索文本框文字、在新窗口中打开链接、检测浏览器、预加载图片等等。具体如下:
禁止右键点击
复制代码代码如下:

$(document).ready(function(){
$(document).bind("contextmenu",function(e){
returnfalse;
});
});

隐藏搜索文本框文字
复制代码代码如下:

$(document).ready(function(){
$("input.text1").val("Enteryoursearchtexthere");
textFill($("input.text1"));
});
functiontextFill(input){//inputfocustextfunction
varoriginalvalue=input.val();
input.focus(function(){
if($.trim(input.val())==originalvalue){input.val("");}
});
input.blur(function(){
if($.trim(input.val())==""){input.val(originalvalue);}
});
}

在新窗口中打开链接
复制代码代码如下:
$(document).ready(function(){
//Example1:Everylinkwillopeninanewwindow
$("a[href^="http://"]").attr("target","_blank");
//Example2:Linkswiththerel="external"attributewillonlyopeninanewwindow
$("a[@rel$="external"]").click(function(){
this.target="_blank";
});
});
//howtouse
<Ahref="http://www.opensourcehunter.com"rel=external>openlink</A>

检测浏览器
注:在版本jQuery1.4中,$.support替换掉了$.browser变量。
复制代码代码如下:
$(document).ready(function(){
//TargetFirefox2andabove
if($.browser.mozilla&&$.browser.version>="1.8"){
//dosomething
}
//TargetSafari
if($.browser.safari){
//dosomething
}
//TargetChrome
if($.browser.chrome){
//dosomething
}
//TargetCamino
if($.browser.camino){
//dosomething
}
//TargetOpera
if($.browser.opera){
//dosomething
}
//TargetIE6andbelow
if($.browser.msie&&$.browser.version<=6){
//dosomething
}
//TargetanythingaboveIE6
if($.browser.msie&&$.browser.version>6){
//dosomething
}
});

预加载图片
复制代码代码如下:
$(document).ready(function(){
jQuery.preloadImages=function()
{
for(vari=0;i").attr("src",arguments[i]);
}
};
//howtouse
$.preloadImages("image1.jpg");
});

页面样式切换
复制代码代码如下:
$(document).ready(function(){
$("a.Styleswitcher").click(function(){
//swicththeLINKRELattributewiththevalueinARELattribute
$("link[rel=stylesheet]").attr("href",$(this).attr("rel"));
});
//howtouse
//placethisinyourheader
<LINKhref="default.css"type=text/cssrel=stylesheet>
//thelinks
<Aclass=Styleswitcherhref="#"rel=default.css>DefaultTheme</A>
<Aclass=Styleswitcherhref="#"rel=red.css>RedTheme</A>
<Aclass=Styleswitcherhref="#"rel=blue.css>BlueTheme</A>
});

列高度相同
如果使用了两个CSS列,使用此种方式可以是两列的高度相同。
复制代码代码如下:
$(document).ready(function(){
functionequalHeight(group){
tallest=0;
group.each(function(){
thisHeight=$(this).height();
if(thisHeight>tallest){
tallest=thisHeight;
}
});
group.height(tallest);
}
//howtouse
$(document).ready(function(){
equalHeight($(".left"));
equalHeight($(".right"));
});
});

动态控制页面字体大小
复制代码代码如下:
$(document).ready(function(){
//Resetthefontsize(backtodefault)
varoriginalFontSize=$("html").css("font-size");
$(".resetFont").click(function(){
$("html").css("font-size",originalFontSize);
});
//Increasethefontsize(biggerfont0
$(".increaseFont").click(function(){
varcurrentFontSize=$("html").css("font-size");
varcurrentFontSizeNum=parseFloat(currentFontSize,10);
varnewFontSize=currentFontSizeNum*1.2;
$("html").css("font-size",newFontSize);
returnfalse;
});
//Decreasethefontsize(smallerfont)
$(".decreaseFont").click(function(){
varcurrentFontSize=$("html").css("font-size");
varcurrentFontSizeNum=parseFloat(currentFontSize,10);
varnewFontSize=currentFontSizeNum*0.8;
$("html").css("font-size",newFontSize);
returnfalse;
});
});

返回页面顶部功能
复制代码代码如下:
$(document).ready(function(){
$("a[href*=#]").click(function(){
if(location.pathname.replace(/^\//,"")==this.pathname.replace(/^\//,"")
&&location.hostname==this.hostname){
var$target=$(this.hash);
$target=$target.length&&$target
||$("[name="+this.hash.slice(1)+"]");
if($target.length){
vartargetOffset=$target.offset().top;
$("html,body")
.animate({scrollTop:targetOffset},900);
returnfalse;
}
}
});
//howtouse
//placethiswhereyouwanttoscrollto
<Aname=top></A>
//thelink
<Ahref="#top">gototop</A>
});

获得鼠标指针XY值
复制代码代码如下:
$(document).ready(function(){
$().mousemove(function(e){
//displaythexandyaxisvaluesinsidethedivwiththeidXY
$("#XY").html("XAxis:"+e.pageX+"|YAxis"+e.pageY);
});
//howtouse
<DIVid=XY></DIV>
});

验证元素是否为空
复制代码代码如下:
$(document).ready(function(){
if($("#id").html()){
//dosomething
}
});

替换元素
复制代码代码如下:
$(document).ready(function(){
$("#id").replaceWith("
<DIV>Ihavebeenreplaced</DIV>
);
});

jQuery延时加载功能
复制代码代码如下:
$(document).ready(function(){
window.setTimeout(function(){
//dosomething
},1000);
});

移除单词功能
复制代码代码如下:
$(document).ready(function(){
varel=$("#id");
el.html(el.html().replace(/word/ig,""));
});

验证元素是否存在于jQuery对象集合中
复制代码代码如下:
$(document).ready(function(){
if($("#id").length){
//dosomething
}
});

使整个DIV可点击
复制代码代码如下:
$(document).ready(function(){
$("div").click(function(){
//gettheurlfromhrefattributeandlaunchtheurl
window.location=$(this).find("a").attr("href");returnfalse;
});
//howtouse
<DIV><Ahref="index.html">home</A></DIV>
});

ID与Class之间转换当改变Window大小时,在ID与Class之间切换
复制代码代码如下:
$(document).ready(function(){
functioncheckWindowSize(){
if($(window).width()>1200){
$("body").addClass("large");
}
else{
$("body").removeClass("large");
}
}
$(window).resize(checkWindowSize);
});

克隆对象
复制代码代码如下:
$(document).ready(function(){
varcloned=$("#id").clone();
//howtouse
<DIVidid=id></DIV>
});

使元素居屏幕中间位置
复制代码代码如下:
$(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");
returnthis;
}
$("#id").center();
});

写自己的选择器
复制代码代码如下:
$(document).ready(function(){
$.extend($.expr[":"],{
moreThen1000px:function(a){
return$(a).width()>1000;
}
});
$(".box:moreThen1000px").click(function(){
//creatingasimplejsalertbox
alert("Theelementthatyouhaveclickedisover1000pixelswide");
});
});

统计元素个数
复制代码代码如下:
$(document).ready(function(){
$("p").size();
});

使用自己的Bullets
复制代码代码如下:
$(document).ready(function(){
$("ul").addClass("Replaced");
$("ul>li").prepend("‒");
//howtouse
ul.Replaced{list-style:none;}
});

引用Google主机上的jQuery类库
复制代码代码如下:
//Example1
<SCRIPTsrc="http://www.google.com/jsapi"></SCRIPT>
<SCRIPTtype=text/javascript>
google.load("jquery","1.2.6");
google.setOnLoadCallback(function(){
//dosomething
});
</SCRIPT><SCRIPTsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"type=text/javascript></SCRIPT>
//Example2:(thebestandfastestway)
<SCRIPTsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"type=text/javascript></SCRIPT>

禁用jQuery(动画)效果
复制代码代码如下:
$(document).ready(function(){
jQuery.fx.off=true;
});

与其他JavaScript类库冲突解决方案
复制代码代码如下:
$(document).ready(function(){
var$jq=jQuery.noConflict();
$jq("#id").show();
});