> ½Å±¾ÓïÑÔ > >
Ç°¶Ë»ù´¡½ø½×£¨Ê®Ò»£©£ºÏêϸͼ½âjQuery¶ÔÏó£¬ÒÔ¼°ÈçºÎÀ©Õ¹jQuery 2017-03-23 15:09¡¡³ö´¦£ºÇåÆÁÍø¡¡ÈËÆø£º¡¡
ÅäͼÓë±¾ÎÄÎÞ¹Ø
Ô缸Äêѧϰǰ¶Ë£¬´ó¼Ò¶¼·Ç³£ÈÈÖÔÓÚÑо¿jQueryÔ´Âë¡£ÎÒ»¹¼ÇµÃµ±³õ´ÓjQueryÔ´ÂëÖÐѧµ½Ò»ÐÇ°ëµãÓ¦Óü¼ÇɵÄʱºò³£»áÓÐÒ»ÖÖ·¢×ÔÄÚÐĵľªÌ¾£¬“ÔÀ´JavaScript¾ÓÈ»¿ÉÒÔÕâÑùÓã¡”
ËäÈ»Ëæ×ÅÇ°¶ËµÄ·¢Õ¹£¬ÁíÍ⼸ÖÖÇ°¶Ë¿ò¼ÜµÄáÈÆð£¬jQueryÂýÂý±äµÃ²»ÔÙÊDZØÐë¡£Òò´Ë´ó¼Ò¶ÔÓÚjQueryµÄÈÈÇéµÍÁ˺ܶࡣµ«ÊÇÐí¶à´ÓjQueryÖÐѧµ½µÄ¼¼ÇÉÓÃÔÚʵ¼Ê¿ª·¢ÖÐÈÔÈ»·Ç³£ºÃÓ᣼òµ¥µÄÁ˽âËüÒ²ÓÐÖúÓÚÎÒÃǸü¼ÓÉîÈëµÄÀí½âJavaScript¡£
ÕâƪÎÄÕµÄÖ÷ҪĿµÄ¾ÍÊǸú´ó¼Ò·ÖÏíһϣ¬jquery¶ÔÏóÊÇÈçºÎ·â×°µÄ¡£ËãÊǶÔÓÚ´ó¼Ò½øÒ»²½Ñ§Ï°jQueryÔ´ÂëµÄÒ»¸öÅ×שÒýÓñ¡£
ʹÓÃjQuery¶ÔÏóʱ£¬ÎÒÃÇÕâÑùд£º
// ÉùÃ÷Ò»¸öjQuery¶ÔÏó $('.target') // »ñÈ¡ÔªËصÄcssÊôÐÔ $('.target').css('width') // »ñÈ¡ÔªËصÄλÖÃÐÅÏ¢ $('.target').offset()ÔÚʹÓÃÖ®³õ¿ÉÄÜ»áÓÐÐí¶àÒÉÎÊ£¬±ÈÈç$ÊÇÔõô»ØÊ£¿ÎªÊ²Ã´²»ÓÃnew¾Í¿ÉÒÔÖ±½ÓÉùÃ÷Ò»¸ö¶ÔÏóµÈµÈ¡£ºóÀ´Á˽âÖ®ºó£¬²ÅÖªµÀÔÀ´ÕâÕýÊÇjQuery¶ÔÏó´´½¨µÄÇÉÃîÖ®´¦¡£
ÏÈÖ±½ÓÓôúÂëչʾ³öÀ´£¬ÔÙÓÃͼ¸ú´ó¼Ò½âÊÍÊÇÔõô»ØÊ¡£
; (function(ROOT) { // ¹¹Ô캯Êý var jQuery = function(selector) { jQuery.fn.init(selector) } jQuery.fn = jQuery.prototype = { constructor: jQuery, version: '1.0.0', init: function(selector) { // ÔÚjqueryÖÐÕâÀïÓÐÒ»¸ö¸´ÔÓµÄÅжϣ¬µ«ÊÇÕâÀïÎÒ×öÁ˼ò»¯ var elem, selector; elem = document.querySelector(selector); this[0] = elem; ; }, // ÔÚÔÐÍÉÏÌí¼ÓÒ»¶Ñ·½·¨ toArray: function() {}, get: function() {}, each: function() {}, ready: function() {}, first: function() {}, slice: function() {} // ... ... } jQuery.fn.init.prototype = jQuery.fn; // ʵÏÖjQueryµÄÁ½ÖÖÀ©Õ¹·½Ê½ jQuery.extend = jQuery.fn.extend = function(options) { // ÔÚjqueryÔ´ÂëÖлá¸ù¾Ý²ÎÊý²»Í¬½øÐкܶàÅжϣ¬ÎÒÃÇÕâÀï¾ÍÖ±½Ó×ßÒ»ÖÖ·½Ê½£¬ËùÒԾͲ»ÓÃÅжÏÁË var target = this; var copy; for(name in options) { copy = options[name]; target[name] = copy; } return target; } // jQueryÖÐÀûÓÃÉÏÃæʵÏÖµÄÀ©Õ¹»úÖÆ£¬Ìí¼ÓÁËÐí¶à·½·¨£¬ÆäÖÐ // Ö±½ÓÌí¼ÓÔÚ¹¹Ô캯ÊýÉÏ£¬±»³ÆΪ¹¤¾ß·½·¨ jQuery.extend({ isFunction: function() {}, type: function() {}, parseHTML: function() {}, parseJSON: function() {}, ajax: function() {} // ... }) // Ìí¼Óµ½ÔÐÍÉÏ jQuery.fn.extend({ queue: function() {}, promise: function() {}, attr: function() {}, prop: function() {}, addClass: function() {}, removeClass: function() {}, val: function() {}, css: function() {} // ... }) // $·ûºÅµÄÓÉÀ´£¬Êµ¼ÊÉÏËü¾ÍÊÇjQuery£¬Ò»¸ö¼ò»¯µÄд·¨£¬ÔÚÕâÀïÎÒÃÇ»¹¿ÉÒÔÌæ»»³ÉÆäËû¿ÉÓÃ×Ö·û ROOT.jQuery = ROOT.$ = jQuery; })(window);ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒ·â×°ÁËÒ»¸ö¼ò»¯°æµÄjQuery¶ÔÏó¡£ËüÏò´ó¼Ò¼òµ¥Õ¹Ê¾ÁËjQueryµÄÕûÌå¿ò¼ÜÇé¿ö¡£Èç¹ûÁ˽âÁËÕûÌå¿ò¼Ü£¬ÄÇô´ó¼ÒÈ¥¶ÁjQueryÔ´Â룬¾Í»á±äµÃ·Ç³£ÇáËÉ¡£
ÎÒÃÇÔÚ´úÂëÖпÉÒÔ¿´µ½£¬jQuery×ÔÉí¶ÔÓÚÔÐ͵Ĵ¦ÀíʹÓÃÁËһЩÇÉÃîµÄÓï·¨£¬±ÈÈç jQuery.fn = jQuery.prototype £¬ jQuery.fn.init.prototype = jQuery.fn; µÈ£¬Õ⼸¾äÕýʽjQuery¶ÔÏóµÄ¹Ø¼üËùÔÚ£¬ÏÂÃæÎÒÓÃͼ¸ø´ó¼ÒչʾһÏÂÕâÖмäµÄÂß¼ÊÇÔõô»ØÊ¡£
jQuery¶ÔÏóºËÐÄͼ
¶ÔÏó·â×°·ÖÎöÔÚÉÏÃæµÄʵÏÖÖУ¬´úÂëÊ×ÏÈÔÚjQuery¹¹Ô캯ÊýÖÐÉùÃ÷ÁËÒ»¸öfnÊôÐÔ£¬²¢½«ÆäÖ¸ÏòÁËÔÐÍ jQuery.prototype ¡£²¢ÔÚÔÐÍÖÐÌí¼ÓÁËinit·½·¨¡£
jQuery.fn = jQuery.prototype = { init: {} }Ö®ºóÓÖ½«initµÄÔÐÍ£¬Ö¸ÏòÁËjQuery.prototype¡£
jQuery.fn.init.prototype = jQuery.fn;¶øÔÚ¹¹Ô캯ÊýjQueryÖУ¬·µ»ØÁËinitµÄʵÀý¶ÔÏó¡£
var jQuery = function(selector) { jQuery.fn.init(selector) }×îºó¶ÔÍⱩ¶Èë¿Úʱ£¬½«×Ö·û $ Óë jQuery ¶ÔµÈÆðÀ´¡£
ROOT.jQuery = ROOT.$ = jQuery;Òò´Ëµ±ÎÒÃÇÖ±½ÓʹÓà $('#test') ´´½¨Ò»¸ö¶ÔÏóʱ£¬Êµ¼ÊÉÏÊÇ´´½¨ÁËÒ»¸öinitµÄʵÀý£¬ÕâÀïµÄÕýÕæ¹¹Ô캯ÊýÊÇÔÐÍÖеÄinit·½·¨¡£
¡¡