Jquery常用功能

jQuery 1.4给开发者带来了很多值得兴奋的新特性,同时使用jQuery的人也越来越多,为了方便大家对jQuery的使用,下面列出了一些jQuery使用技巧。比如有禁止右键点击、隐藏搜索文本框文字、在新窗口中打开链接、检测浏览器、预加载图片等等。具体如下:

禁止右键点击

  1. $(document).ready(function(){     
  2. $(document).bind("contextmenu",function(e){     
  3.    return false;     
  4.   });     
  5. });   

隐藏搜索文本框文字

  1. $(document).ready(function() {     
  2. $("input.text1").val("Enter your search text here");     
  3.    textFill($('input.text1'));     
  4. });     
  5. function textFill(input){ //input focus text function     
  6.    var originalvalue = input.val();     
  7.    input.focus( function(){     
  8.  
  9. if( $.trim(input.val()) == originalvalue ){ input.val(''); }     
  10. });     
  11.  
  12. input.blur( function(){     
  13.     if( $.trim(input.val()) == '' ){ input.val(originalvalue); }     
  14. });     
  15. }    

在新窗口中打开链接

  1. $(document).ready(function() {     
  2. //Example 1: Every link will open in a new window     
  3. $('a[href^="http://"]').attr("target", "_blank");     
  4. //Example 2: Links with the rel="external" attribute will only open in a new window     
  5. $('a[@rel$='external']').click(function(){     
  6.      this.target = "_blank";     
  7. });     
  8.  
  9. });     
  10. // how to use     
  11. <A href="http://www.opensourcehunter.com" rel=external>open link</A> 

检测浏览器

注: 在版本jQuery 1.4中,$.support 替换掉了$.browser 变量。

  1. $(document).ready(function() {     
  2. // Target Firefox 2 and above     
  3.    if ($.browser.mozilla && $.browser.version >= "1.8" ){     
  4.     // do something     
  5.  
  6.    }     
  7. // Target Safari     
  8. if( $.browser.safari ){     
  9. // do something     
  10. }     
  11. // Target Chrome     
  12. if( $.browser.chrome){     
  13. // do something     
  14.  
  15. }     
  16. // Target Camino     
  17. if( $.browser.camino){     
  18. // do something     
  19.  
  20. }     
  21. // Target Opera     
  22. if( $.browser.opera){     
  23. // do something     
  24.  
  25. }     
  26. // Target IE6 and below     
  27. if ($.browser.msie && $.browser.version <= 6 ){     
  28. // do something     
  29.  
  30. }     
  31. // Target anything above IE6     
  32. if ($.browser.msie && $.browser.version > 6){     
  33. // do something     
  34. }     
  35. });   

预加载图片

  1. $(document).ready(function() {     
  2.  
  3.   jQuery.preloadImages = function()     
  4.   {     
  5.      for(var i = 0; i").attr("src", arguments[i]);    
  6.  
  7.   }    
  8. };    
  9. // how to use    
  10. $.preloadImages("image1.jpg");     
  11. });   

页面样式切换

  1. $(document).ready(function() {     
  2.  
  3. $("a.Styleswitcher").click(function() {     
  4.  //swicth the LINK REL attribute with the value in A REL attribute     
  5. $('link[rel=stylesheet]').attr('href' , $(this).attr('rel'));     
  6.  
  7.   });     
  8.  
  9. // how to use     
  10. // place this in your header     
  11.  
  12. <LINK href="default.css" type=text/css rel=stylesheet>     
  13. // the links     
  14. <A class=Styleswitcher href="#" rel=default.css>Default Theme</A>     
  15. <A class=Styleswitcher href="#" rel=red.css>Red Theme</A>     
  16. <A class=Styleswitcher href="#" rel=blue.css>Blue Theme</A>     
  17. });  

原文地址:http://www.cnblogs.com/xiaopin/archive/2010/11/13/1876366.html

原文地址:https://www.cnblogs.com/yznyzcw/p/3988612.html