几个有用的jQuery技巧

来源:互联网 发布:轴承数据库 编辑:程序博客网 时间:2024/05/22 02:06

作为轻量级的JS库,jQuery随着JavaScrīpt脚本的大热而备受Web开发者亲睐。下面技巧实现的效果虽然并不新鲜,但通过jQuery的封装,HTML实现了很大的清洁。清爽简洁又高效的代码任何时候都是开发者所醉心追求的终极目标,也许它简单,但是它能量巨大。这里整理jQuery优化系列的五个jQuery技巧。

一、字体大小的调整。

允许用户在访问站点时能自由地调节页面字体大小,将带来很好的用户体验。下面的代码就是jQuery要告诉我们怎样做到这点的:

  1. //check that the DOM is ready 
  2. $(document).ready(function() { 
  3.     //get the current font size 
  4.     var originalFontSize = $('html').css('font-size'); 
  5.  
  6.     //Increase the font size 
  7.     $(".increaseFont").click(function() { 
  8.         var currentFontSize = $('html').css('font-size'); 
  9.         var currentFontSizeNumber = parseFloat(currentFontSize, 10); 
  10.         //increases the font- could be set to a value from  
  11.         //the user as well 
  12.         var newFontSize = currentFontSizeNumber*1.2; 
  13.         $('html').css('font-size', newFontSize); 
  14.         return false
  15.     }); 
  16.  
  17.     //Decrease the Font Size 
  18.     $(".decreaseFont").click(function() { 
  19.         var currentFontSize = $('html').css('font-size'); 
  20.         var currentFontSizeNum = parseFloat(currentFontSize, 10); 
  21.         //decreases font.  Could be set to a value from 
  22.         //the user as well 
  23.         var newFontSize = currentFontSizeNum*0.8; 
  24.         $('html').css('font-size', newFontSize); 
  25.         return false
  26.     }); 
  27.  
  28.     // Reset Font Size 
  29.     $(".resetFont").click(function(){ 
  30.     $('html').css('font-size', originalFontSize); 
  31.   }); 
  32. }); 

建立增减字体大小的样式。

二、在新窗口打开链接

为了让访问者尽量停留在自己的站点上,我们通常会设置在新窗口打开所有外部链接。但在XHTML 1.0中,是没有“_blank”标签属性的。在这类情况下,使用以下jQuery技巧就可以避免这个问题,并能在新窗口中打开所有外部链接。

  1. //check that the DOM is ready 
  2. $(document).ready(function() { 
  3.     //select all anchor tags that have http in the href 
  4.     //and apply the target=_blank 
  5.     $("a[href^='http']").attr('target','_blank'); 
  6. }); 

这样就行了!现在,所有的外部链接都可以从一个新窗口打开,以便用户留在原页面。如果原页面使用了很多外部文档的链接,如PDF或DOC文件,那我们可以创建一些规则以便在新窗口中加载这些文件。

三、交换样式表

除了允许访问者改变页面字体大小,还可以允许访问者通过选择不同的页面主题样式来感受不同的页面风格。

  1. //check that the DOM is ready 
  2. $(document).ready(function() { 
  3.     $("a.cssSwap").click(function() {  
  4.         //swap the link rel attribute with the value in the rel     
  5.         $('link[rel=stylesheet]').attr('href' , $(this).attr('rel'));  
  6.     });  
  7. }); 

四、禁用右键

通常禁用右键是为了防止访问者直接从页面拷贝信息,或者是想创建自己独特的右键功能。当然,不管什么原因,禁用右键可以通过以下代码实现:

  1. //check that the DOM is ready 
  2. $(document).ready(function() { 
  3.     //catch the right-click context menu 
  4.     $(document).bind("contextmenu",function(e) {                
  5.         //warning prompt - optional 
  6.         alert("No right-clicking!"); 
  7.                       
  8.         //cancel the default context menu 
  9.         return false
  10.     }); 
  11. }); 

jQuery使我们更容易使用右键来对网页进行处理。

五、返回顶部链接

如果页面过长,可能通过增加“返回顶部”的链接来使访问者方便地返回页面顶部。这是一个简单的JavaScript效果,我们可以通过jQuery运用滚动效果增添一点点小技巧。

  1. $(document).ready(function() { 
  2.     //when the id="top" link is clicked 
  3.     $('#top').click(function() { 
  4.         //scoll the page back to the top 
  5.         $(document).scrollTo(0,500); 
  6.     } 
  7. }); 

对于拥有长页面的站点来说,这真是一个必备功能。

当成为一个jQuery熟手时,一定会发现更多的诸如此类的开发技巧。加油!



原创粉丝点击