打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
每个程序员都会的 35 个 jQuery 小技巧(3)

21. 验证元素是否为空

  1. This will allow you to check if an element is empty. 
  2.  
  3. $(document).ready(function() { 
  4.   if ($('#id').html()) { 
  5.    // do something 
  6.    } 
  7. }); 

22. 替换元素

  1. $(document).ready(function() { 
  2.    $('#id').replaceWith(' 
  3. <DIV>I have been replaced</DIV> 
  4.  
  5. '); 
  6. }); 

23. jQuery延时加载功能

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

24. 移除单词功能

  1. $(document).ready(function() { 
  2.    var el = $('#id'); 
  3.    el.html(el.html().replace(/word/ig, "")); 
  4. }); 

25. 验证元素是否存在于jquery对象集合中

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

26. 使整个DIV可点击

  1. $(document).ready(function() { 
  2.     $("div").click(function(){ 
  3.       //get the url from href attribute and launch the url 
  4.       window.location=$(this).find("a").attr("href"); return false; 
  5.     }); 
  6. // how to use 
  7. <DIV><A href="index.html">home</A></DIV> 
  8.  
  9. }); 

27. ID与Class之间转换

当改变Window大小时,在ID与Class之间切换

  1. $(document).ready(function() { 
  2.    function checkWindowSize() { 
  3.     if ( $(window).width() > 1200 ) { 
  4.         $('body').addClass('large'); 
  5.     } 
  6.     else { 
  7.         $('body').removeClass('large'); 
  8.     } 
  9.    } 
  10. $(window).resize(checkWindowSize); 
  11. }); 

28. 克隆对象

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

29. 使元素居屏幕中间位置

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

30. 写自己的选择器

  1. $(document).ready(function() { 
  2.    $.extend($.expr[':'], { 
  3.        moreThen1000px: function(a) { 
  4.            return $(a).width() > 1000; 
  5.       } 
  6.    }); 
  7.   $('.box:moreThen1000px').click(function() { 
  8.       // creating a simple js alert box 
  9.       alert('The element that you have clicked is over 1000 pixels wide'); 
  10.   }); 
  11. }); 

31. 统计元素个数

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

32. 使用自己的 Bullets

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

33. 引用Google主机上的Jquery类库

  1. //Example 1 
  2. <SCRIPT src="//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 type=text/javascript src="//ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></SCRIPT> 
  9.  
  10. // Example 2:(the best and fastest way) 
  11. <SCRIPT type=text/javascript src="//ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></SCRIPT> 

34. 禁用Jquery(动画)效果

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

35. 与其他Javascript类库冲突解决方案

  1. $(document).ready(function() { 
  2.    var $jq = jQuery.noConflict(); 
  3.    $jq('#id').show(); 
  4. }); 

【编辑推荐】


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
14个有用的Jquery技巧分享
jQuery 1.4实用技巧大放送
jquery26个技巧
jquery
Jquery与Prototype混合用法对比
jquery 简单学习系列
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服