jQuery扩展方法 (插件机制)

2021/5/11 18:27:02

本文主要是介绍jQuery扩展方法 (插件机制),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  • jQuery.extend(object)
    扩展jQuery对象本身。
    
    用来在jQuery命名空间上增加新函数。 
    
    在jQuery命名空间上增加两个函数:
    
    
    <script>
        jQuery.extend({
          min: function(a, b) { return a < b ? a : b; },
          max: function(a, b) { return a > b ? a : b; }
    });
    
        jQuery.min(2,3); // => 2
        jQuery.max(4,5); // => 5
    </script>
  • jQuery.fn.extend(object)
    扩展 jQuery 元素集来提供新的方法(通常用来制作插件)
    
    增加两个插件方法:
    
    <body>
    
    <input type="checkbox">
    <input type="checkbox">
    <input type="checkbox">
    
    <script src="jquery.min.js"></script>
    <script>
        jQuery.fn.extend({
          check: function() {
             $(this).attr("checked",true);
          },
          uncheck: function() {
             $(this).attr("checked",false);
          }
        });
    
        $(":checkbox").check()
    </script>
    
    </body>

     



这篇关于jQuery扩展方法 (插件机制)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程