0 Comments

jQuery 1.4实用技巧大放送(3)

发布于:2013-06-04  |   作者:广州网站建设  |   已聚集:人围观

jQuery延时加载功能


  1. $(document).ready(function() {     
  2.  
  3.     window.setTimeout(function() {     
  4.  
  5.     // do something     
  6.  
  7.      }, 1000);     
  8.  
  9. });   

移除单词功能

广州网站建设,网站建设,广州网页设计,广州网站设计
  1. $(document).ready(function() {     
  2.  
  3.    var el = $('#id');     
  4.  
  5.    el.html(el.html().replace(/word/ig, ""));     
  6.  
  7. });   

验证元素是否存在于jQuery对象集合中


  1. $(document).ready(function() {     
  2.  
  3.    if ($('#id').length) {     
  4.  
  5.    // do something     
  6.  
  7. }     
  8. });   

使整个DIV可点击

广州网站建设,网站建设,广州网页设计,广州网站设计
  1. $(document).ready(function() {     
  2.  
  3.   $("div").click(function(){     
  4.  
  5.   //get the url from href attribute and launch the url     
  6.  
  7.      window.location=$(this).find("a").attr("href"); return false;     
  8.  
  9. });     
  10.  
  11. // how to use     
  12.  
  13. <DIV><A href="index.html">home</A></DIV>     
  14. });    
  15.  
  16. ID与Class之间转换当改变Window大小时,在ID与Class之间切换  
  17.  
  18. $(document).ready(function() {     
  19.  
  20.    function checkWindowSize() {     
  21.    if ( $(window).width() > 1200 ) {     
  22.  
  23.      $('body').addClass('large');     
  24.  
  25.    }     
  26.    else {     
  27.  
  28.       $('body').removeClass('large');     
  29.  
  30.         }     
  31.  
  32. }     
  33. $(window).resize(checkWindowSize);     
  34. });   

克隆对象


  1. $(document).ready(function() {     
  2.  
  3.   var cloned = $('#id').clone();     
  4.  
  5.   // how to use     
  6.  
  7. <DIV idid=id></DIV>     
  8. });   

使元素居屏幕中间位置


  1. $(document).ready(function() {     
  2.  
  3.   jQuery.fn.center = function () {     
  4.  
  5.   this.css("position","absolute");     
  6.  
  7.   this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");     
  8.  
  9.   this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");     
  10.  
  11.      return this;     
  12.  
  13. }     
  14.  
  15. $("#id").center();     
  16.  
  17. });   

写自己的选择器

广州网站建设,网站建设,广州网页设计,广州网站设计
  1. $(document).ready(function() {     
  2.  
  3.    $.extend($.expr[':'], {     
  4.  
  5.      moreThen1000px: function(a) {     
  6.  
  7.    return $(a).width() > 1000;     
  8.  
  9.   }     
  10.  
  11. });     
  12. $('.box:moreThen1000px').click(function() {     
  13.  
  14.   // creating a simple js alert box     
  15.  
  16.     alert('The element that you have clicked is over 1000 pixels wide');     
  17.  
  18.   });     
  19.  
  20. });   

统计元素个数


  1. $(document).ready(function() {     
  2. $("p").size();     
  3. });   

使用自己的Bullets


  1. $(document).ready(function() {     
  2.   $("ul").addClass("Replaced");     
  3.   $("ul > li").prepend("‒ ");     
  4.   // how to use     
  5.   ul.Replaced { list-style : none; }     
  6.  
  7. });   

引用Google主机上的jQuery类库


  1. //Example 1     
  2. <SCRIPT src="http://www.google.com/jsapi"></SCRIPT>     
  3. <SCRIPT type=text/javascript>    
  4.    google.load("jquery", "1.2.6");    
  5.    google.setOnLoadCallback(function() {    
  6. // do something    
  7. });    
  8. </SCRIPT><SCRIPT src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type=text/javascript></SCRIPT>     
  9.  
  10. // Example 2:(the best and fastest way)     
  11. <SCRIPT src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type=text/javascript></SCRIPT>  

禁用jQuery(动画)效果


  1. $(document).ready(function() {     
  2.  
  3.    jQuery.fx.off = true;     
  4.  
  5. });   

与其他JavaScript类库冲突解决方案


  1. $(document).ready(function() {     
  2.  
  3.   var $jq = jQuery.noConflict();     
  4.  
  5.   $jq('#id').show();     
  6.  
  7. });  
标签:
飞机