分享12个实用的jQuery代码片段
jQuery是一款优秀的JavaScript库,它在WEB开发者和网页设计师中非常出名,帮助网页设计师开发出很多有创意和漂亮的WEB页面。
本文主要分享了12个有用的jQuery技巧,具体内容如下
下面是我收集的一些小技巧,这些技巧将帮助你提高你网站布局和应用的创意性以及功能性。
一、在新窗口打开链接
用这个代码,你点击超文本链接时会在新窗口中打开,为用户带来更好的体验:
$(document).ready(function(){ //selectallanchortagsthathavehttpinthehref //andapplythetarget=_blank $("a[href^='http']").attr('target','_blank'); });
二、设定计时器
$(document).ready(function(){ window.setTimeout(function(){ //somecode },500); });
三、设置等高的列
使用下面的代码,可以使得你的网页应用每一列高度都一样:
<divclass="equalHeight"style="border:1pxsolid"> <p>FirstLine</p> <p>SecondLine</p> <p>ThirdLine</p> </div> <divclass="equalHeight"style="border:1pxsolid"> <p>ColumnTwo</p> </div> <scriptsrc="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function(){ equalHeight('.equalHeight'); }); //globalvariable,thiswillstorethehighestheightvalue varmaxHeight=0; functionequalHeight(col){ //Getalltheelementwithclass=col col=$(col); //Loopallthecol col.each(function(){ //Storethehighestvalue if($(this).height()>maxHeight){ maxHeight=$(this).height(); } }); //Settheheight col.height(maxHeight); } </script>
四、jQuery预加载图像
这个技巧可以加快网页加载图片的速度:
jQuery.preloadImagesInWebPage=function(){ for(varctr=0;ctr<arguments.length;ctr++){ jQuery("").attr("src",arguments[ctr]); } } //使用方法: $.preloadImages("image1.gif","image2.gif","image3.gif"); //检查图片是否被加载 $('#imageObject').attr('src','image1.gif').load(function(){ alert('Theimagehasbeenloaded…'); });
五、把元素定位到页面中间
<divid="foo"style="width:200px;height:200px;background:#ccc;"></div> <scriptsrc="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script> <scripttype="text/javascript"> 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; } //Usetheabovefunctionas: $('#foo').center(); </script>
六、禁用鼠标右键
$(document).ready(function(){ //catchtheright-clickcontextmenu $(document).bind("contextmenu",function(e){ //warningprompt-optional alert("Noright-clicking!"); //deletethedefaultcontextmenu returnfalse; }); });
七、计算子元素的个数
<divid="foo"> <divid="bar"></div> <divid="baz"> <divid="biz"></div> <span><span></span></span> </div> </div> <scriptsrc="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script> <scripttype="text/javascript"> //jQuerycodetocountchildelements$("#foo>div").size() alert($("#foo>div").size()) </script>
八、更改样式列表
这段代码将帮助你更改样式列表。
$(document).ready(function(){ $("a.cssSwap").click(function(){ //swapthelinkrelattributewiththevalueintherel $('link[rel=stylesheet]').attr('href',$(this).attr('rel')); }); });
九、使用jQuery设定文本大小
加入这段代码,用户可根据需求重新设定文本尺寸(增加或减少)。
$(document).ready(function(){ //findthecurrentfontsize varoriginalFontSize=$('html').css('font-size'); //Increasethetextsize $(".increaseFont").click(function(){ varcurrentFontSize=$('html').css('font-size'); varcurrentFontSizeNumber=parseFloat(currentFontSize,10); varnewFontSize=currentFontSizeNumber*1.2; $('html').css('font-size',newFontSize); returnfalse; }); //DecreasetheTextSize $(".decreaseFont").click(function(){ varcurrentFontSize=$('html').css('font-size'); varcurrentFontSizeNum=parseFloat(currentFontSize,10); varnewFontSize=currentFontSizeNum*0.8; $('html').css('font-size',newFontSize); returnfalse; }); //ResetFontSize $(".resetFont").click(function(){ $('html').css('font-size',originalFontSize); }); });
十、检测当前鼠标坐标
使用这个JS代码,你可以在任何浏览器里获取鼠标坐标。
$(document).ready(function(){ $().mousemove(function(e) { $('#MouseCoordinates').html("XAxisPosition="+e.pageX+"andYAxisPosition="+e.pageY); });
十一、获取鼠标指针的X/Y轴
$().mousemove(function(e){ //displaythexandyaxisvaluesinsidethePelement $('p').html("XAxis:"+e.pageX+"|YAxis"+e.pageY); });
十二、返回到顶部链接
此代码对于比较长的页面非常实用,用户不必拉滚动条来返回顶部,直接点击“返回顶部”即可。
$(document).ready(function(){ //whentheid="top"linkisclicked $('#top').click(function(){ //scollthepagebacktothetop $(document).scrollTo(0,500); } });
jQuery是JavaScript最好的库之一,支持Ajax及HTML脚本客户端,主要用于事件处理及制作动画。另外,jQuery还拥有各种插件,可以让开发者在最快的时间内创建网页。
希望本文所述对大家学习jquery程序设计有所帮助。