jQuery provides two methods for developing plug-ins,
jQuery.fn.extend();
jQuery.extend();
jQuery.fn = jQuery.prototype;
$("#btn1") will generate an instance of the jQuery class, that is, you can directly call the methods in jQuery.prototype.
For jQuery.extend:
1. jquery.extend(obj) : Add a class method to the jQuery class, which can be understood as adding a static method
Such as:
- 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
- var settings = { validate: false, limit: 5, name: "foo" };
- var options = { validate: true, name: "bar" };
- jQuery.extend(settings, options);
- 结果:settings == { validate: true, limit: 5, name: "bar" }
For jQuery.fn.extend: The extension of jQuery.prototype is to add "member functions" to the jQuery class. Instances of the jQuery class can use this "member function". Extend an object to jQuery's prototype, which is the plug-in mechanism
- $.fn.extend({
- alertWhileClick:function() {
- $(this).click(function(){
- alert($(this).val());
- });
- }
- });
- $("#input1").alertWhileClick();
jQuery provides two methods for developing plug-ins,
jQuery.fn.extend();
jQuery.extend();
jQuery.fn = jQuery.prototype;
$("#btn1") will generate an instance of the jQuery class, that is, you can directly call the methods in jQuery.prototype.
For jQuery.extend:
1. jquery.extend(obj) : Add a class method to the jQuery class, which can be understood as adding a static method
Such as:
- 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
- var settings = { validate: false, limit: 5, name: "foo" };
- var options = { validate: true, name: "bar" };
- jQuery.extend(settings, options);
- 结果:settings == { validate: true, limit: 5, name: "bar" }
For jQuery.fn.extend: The extension of jQuery.prototype is to add "member functions" to the jQuery class. Instances of the jQuery class can use this "member function". Extend an object to jQuery's prototype, which is the plug-in mechanism
- $.fn.extend({
- alertWhileClick:function() {
- $(this).click(function(){
- alert($(this).val());
- });
- }
- });
- $("#input1").alertWhileClick();