幾個有用的jQuery技巧

作爲輕量級的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熟手時,一定會發現更多的諸如此類的開發技巧。加油!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章