6 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;
    		}
    	});
    	console.log($.min(3,5)); //3
    	console.log($.max(3,5)); //5
    </script>
    

  • jQuery.fn.extend(object)

    扩展jQuery元素集来提供新的方法(通常用来制作插件)
    增加两个插件方法:
    
    <input type="checkbox">
    <input type="checkbox">
    <input type="checkbox">
    <script>
    	jQuery.fn.extend({
    		check: function () {
    			$(this).attr('checked',true);
    		},
    		uncheck: function () {
    			$(this).attr('checked',false);
    		}
    	});
    		$(':checkbox').check()
    </script>
    
posted @ 2022-08-05 17:26  角角边  Views(25)  Comments(0)    收藏  举报