jQueryµÄ.bind()¡¢.live()ºÍ.delegate()Ö®¼äÇø±ð
DOMÊ÷
Ê×ÏÈ£¬¿ÉÊÓ»¯Ò»¸öHMTLÎĵµµÄDOMÊ÷ÊǺÜÓаïÖúµÄ¡£Ò»¸ö¼òµ¥µÄHTMLÒ³Ãæ¿´ÆðÀ´¾ÍÏñÊÇÕâ¸öÑù×Ó£º
ʼþðÅÝ(ÓÖ³Æʼþ´«²¥)
µ±ÎÒÃǵã»÷Ò»¸öÁ´½Óʱ£¬Æä´¥·¢ÁËÁ´½ÓÔªËصĵ¥»÷ʼþ£¬¸ÃʼþÔòÒý·¢ÈκÎÎÒÃÇÒѰ󶨵½¸ÃÔªËصĵ¥»÷ʼþÉϵĺ¯ÊýµÄÖ´ÐС£
1 $('a').bind('click',function(){alert('that tickles!')})
Òò´ËÒ»¸öµ¥»÷²Ù×÷»á´¥·¢alertº¯ÊýµÄÖ´ÐС£
clickʼþ½Ó×Å»áÏòÊ÷µÄ¸ù·½Ïò´«²¥£¬¹ã²¥µ½¸¸ÔªËØ£¬È»ºó½Ó×ÅÊÇÿ¸ö×æÏÈÔªËØ£¬Ö»ÒªÊÇËüµÄij¸öºó´úÔªËØÉϵĵ¥»÷ʼþ±»´¥·¢£¬Ê¼þ¾Í»á´«¸øËü¡£
ÔÚ²Ù×ÝDOMµÄÓï¾³ÖУ¬documentÊǸù½Úµã¡£
ÏÖÔÚÎÒÃÇ¿ÉÒÔ½ÏÈÝÒ×µØ˵Ã÷.bind()¡¢.live()ºÍ.delegate()µÄ²»Í¬Ö®´¦ÁË¡£
.bind()
1 $('a').bind('click',function(){alert('That tickles!');})
ÕâÊÇ×î¼òµ¥µÄ°ó¶¨·½·¨ÁË¡£JQueryɨÃèÎĵµÕÒ³öËùÓеÄ$(¡®a¡¯)ÔªËØ£¬²¢°Ñalertº¯Êý°ó¶¨µ½Ã¿¸öÔªËصÄclickʼþÉÏ¡£
.live()
$('a').live('click',function(){alert('That tickles!')})
JQuery°Ñalertº¯Êý°ó¶¨µ½$(document)ÔªËØÉÏ£¬²¢Ê¹Óá¯click¡¯ºÍ¡¯a¡¯×÷Ϊ²ÎÊý¡£ÈκÎʱºòÖ»ÒªÓÐʼþðÅݵ½
document½ÚµãÉÏ£¬Ëü¾Í²é¿´¸ÃʼþÊÇ·ñÊÇÒ»¸öclickʼþ£¬ÒÔ¼°¸ÃʼþµÄÄ¿±êÔªËØÓ롯a¡¯ÕâÒ»CSSÑ¡ÔñÆ÷ÊÇ·ñÆ¥Å䣬Èç¹û¶¼ÊǵĻ°£¬ÔòÖ´Ðк¯Êý¡£
live·½·¨»¹¿ÉÒÔ±»°ó¶¨µ½¾ßÌåµÄÔªËØ(»ò¡°context¡±)¶ø²»ÊÇdocumentÉÏ£¬ÏñÕâÑù£º
$('a',$('#container')[0]).live('click',function(){alert('That tickles!')})
.delegate()
$('#container').delegate('a','click',function(){alert('That tickles!')})
JQueryɨÃèÎĵµ²éÕÒ$('#container')£¬²¢Ê¹ÓÃclickʼþºÍ'a'ÕâÒ»CSSÑ¡ÔñÆ÷×÷Ϊ²ÎÊý°Ñalertº¯Êý°ó¶¨
µ½$('#container')ÉÏ¡£ÈκÎʱºòÖ»ÒªÓÐʼþðÅݵ½$('#container')ÉÏ£¬Ëü¾Í²é¿´¸ÃʼþÊÇ·ñÊÇclickʼþ£¬ÒÔ¼°¸ÃʼþµÄÄ¿
±êÔªËØÊÇ·ñÓëCSSÑ¡ÔñÆ÷ÏàÆ¥Åä¡£Èç¹ûÁ½ÖÖ¼ì²éµÄ½á¹û¶¼ÎªÕæµÄ»°£¬Ëü¾ÍÖ´Ðк¯Êý¡£
¿ÉÒÔ×¢Òâµ½£¬ÕâÒ»¹ý³ÌÓë.live()ÀàËÆ£¬µ«ÊÇÆä°Ñ´¦Àí³ÌÐò°ó¶¨µ½¾ßÌåµÄÔªËضø·ÇdocumentÕâÒ»¸ùÉÏ¡£¾«Ã÷µÄJS¡¯erÃÇ¿ÉÄÜ»á×ö³öÕâ
ÑùµÄ½áÂÛ£¬¼´$('a').live() == $(document).delegate('a')£¬ÊÇÕâÑùÂð?àÅ£¬²»£¬²»ÍêÈ«ÊÇ¡£
Ϊʲô.delegate()Òª±È.live()ºÃÓÃ
»ùÓÚ¼¸¸öÔÒò£¬ÈËÃÇͨ³£¸üÔ¸ÒâÑ¡ÓÃjQueryµÄdelegate·½·¨¶ø²»ÊÇlive·½·¨¡£¿¼ÂÇÏÂÃæµÄÀý×Ó£º
$('a').live('click', function() { blah() });
»ò
$(document).delegate('a', 'click', function() { blah() });
ºóÕßʵ¼ÊÉÏÒª¿ì¹ýÇ°Õߣ¬ÒòΪǰÕßÊ×ÏÈҪɨÃèÕû¸öµÄÎĵµ²éÕÒËùÓеÄ$(¡®a¡¯)ÔªËØ£¬°ÑËüÃÇ´æ³ÉjQuery¶ÔÏó¡£¾¡¹Üliveº¯Êý½öÐèÒª°Ñ¡¯a¡¯×÷Ϊ´®²ÎÊý´«µÝÒÔÓÃ×öÖ®ºóµÄÅжϣ¬µ«ÊÇ$()º¯Êý²¢Î´¡°ÖªµÀ¡±±»Á´½ÓµÄ·½·¨½«»áÊÇ.live()¡£
¶øÁíÒ»·½Ã棬delegate·½·¨½öÐèÒª²éÕÒ²¢´æ´¢$(document)ÔªËØ¡£
Ò»ÖÖÑ°Çó±Ü¿ªÕâÒ»ÎÊÌâµÄ·½·¨Êǵ÷ÓÃÔÚ$(document).ready()Ö®Íâ°ó¶¨µÄlive£¬ÕâÑùËü¾Í»áÁ¢¼´Ö´ÐС£ÔÚÕâÖÖ·½Ê½Ï£¬Æä»áÔÚDOM»ñµÃÌî³ä֮ǰÔËÐУ¬Òò´Ë¾Í²»»á²éÕÒÔªËØ»òÊÇ´´½¨jQuery¶ÔÏóÁË¡£
Áé»îÐÔºÍÁ´ÄÜÁ¦
liveº¯ÊýҲͦÁîÈ˷ѽâµÄ¡£ÏëÏë¿´£¬Ëü±»Á´µ½$(¡®a¡¯)¶ÔÏó¼¯ÉÏ£¬µ«Æäʵ¼ÊÉÏÊÇÔÚ$(document)¶ÔÏóÉÏ·¢Éú×÷Óá£ÓÉÓÚÕâ¸öÔÒò£¬Ëü
Äܹ»ÊÔͼÒÔÒ»ÖÖÏÅËÀÈ˵ķ½Ê½À´°Ñ·½·¨Á´µ½×ÔÉíÉÏ¡£Êµ¼ÊÉÏ£¬ÎÒÏë˵µÄÊÇ£¬ÒÔ$.live(¡®a¡¯,¡)ÕâÒ»ÐÎʽ×÷ΪһÖÖÈ«¾ÖÐÔµÄjQuery·½·¨£¬live
·½·¨»á¸ü¾ßÒâÒåһЩ¡£
½öÖ§³ÖCSSÑ¡ÔñÆ÷
×îºóÒ»µã£¬live·½·¨ÓÐÒ»¸ö·Ç³£´óµÄȱµã£¬ÄǾÍÊÇËü½öÄÜÕë¶ÔÖ±½ÓµÄCSSÑ¡ÔñÆ÷×ö²Ù×÷£¬ÕâʹµÃËü±äµÃ·Ç³£µÄ²»Áé»î¡£
ÓûÁ˽â¸ü¶à¹ØÓÚCSSÑ¡ÔñÆ÷µÄȱµã£¬Çë²ÎÔÄExploring jQuery .live() and .die()Ò»ÎÄ¡£
¸üУº¸ÐлHacker NewsÉϵÄpedalpeteºÍºóÃæÆÀÂÛÖеÄEllsassÌáÐÑÎÒ¼ÓÈë½ÓÏÂÀ´µÄÕâÒ»½ÚÄÚÈÝ¡£
ΪʲôѡÔñ.live()»ò.delegate()¶ø²»ÊÇ.bind()
±Ï¾¹£¬bind¿´ÆðÀ´Ëƺõ¸ü¼ÓµÄÃ÷È·ºÍÖ±½Ó£¬ÄѵÀ²»ÊÇÂð?àÅ£¬ÓÐÁ½¸öÔÒòÈÃÎÒÃǸüÔ¸ÒâÑ¡Ôñdelegate»òlive¶ø²»ÊÇbind£º
1. ΪÁË°Ñ´¦Àí³ÌÐò¸½¼Óµ½¿ÉÄÜ»¹Î´´æÔÚÓÚDOMÖеÄDOMÔªËØÖ®ÉÏ¡£ÒòΪbindÊÇÖ±½Ó°Ñ´¦Àí³ÌÐò°ó¶¨µ½¸÷¸öÔªËØÉÏ£¬Ëü²»ÄÜ°Ñ´¦Àí³ÌÐò°ó¶¨µ½»¹Î´´æÔÚÓÚÒ³ÃæÖеÄÔªËØÖ®ÉÏ¡£
2.
Èç¹ûÄãÔËÐÐÁË$('a').bind(¡)£¬¶øºóеÄÁ´½Ó¾ÓÉAJAX¼ÓÈëµ½ÁËÒ³ÃæÖУ¬ÔòÄãµÄbind´¦Àí³ÌÐò¶ÔÓÚÕâЩмÓÈëµÄÁ´½ÓÀ´ËµÊÇÎÞЧµÄ¡£¶øÁíÒ»
·½ÃæliveºÍdelegateÔòÊDZ»°ó¶¨µ½ÁíÒ»¸ö×æÏȽڵãÉÏ£¬Òò´ËÆä¶ÔÓÚÈκÎÄ¿Ç°»òÊǽ«À´´æÔÚÓÚ¸Ã×æÏÈÔªËØÖ®ÄÚµÄÔªËض¼ÊÇÓÐЧµÄ¡£
3.
»òÕßΪÁË°Ñ´¦Àí³ÌÐò¸½¼Óµ½µ¥¸öÔªËØÉÏ»òÊÇһС×éÔªËØÖ®ÉÏ£¬¼àÌýºó´úÔªËØÉϵÄʼþ¶ø²»ÊÇÑ»·±éÀú²¢°Ñͬһ¸öº¯ÊýÖð¸ö¸½¼Óµ½DOMÖеÄ100¸öÔªËØÉÏ¡£°Ñ´¦Àí
³ÌÐò¸½¼Óµ½Ò»¸ö(»òÊÇһС×é)×æÏÈÔªËØÉ϶ø²»ÊÇÖ±½Ó°Ñ´¦Àí³ÌÐò¸½¼Óµ½Ò³ÃæÖеÄËùÓÐÔªËØÉÏ£¬ÕâÖÖ×ö·¨´øÀ´ÁËÐÔÄÜÉϵĺô¦¡£
Í£Ö¹´«²¥
×îºóÒ»¸öÎÒÏë×öµÄÌáÐÑÓëʼþ´«²¥Óйء£Í¨³£Çé¿öÏ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýʹÓÃÕâÑùµÄʼþ·½·¨À´ÖÕÖ¹´¦Àíº¯ÊýµÄÖ´ÐУº
$('a').bind('click',function(e){ e.preventDefault() e.stopPropagation() })
²»¹ý£¬µ±ÎÒÃÇʹÓÃlive»òÊÇdelegate·½·¨µÄʱºò£¬´¦Àíº¯Êýʵ¼ÊÉϲ¢Ã»ÓÐÔÚÔËÐУ¬ÐèÒªµÈµ½Ê¼þðÅݵ½´¦Àí³ÌÐòʵ¼Ê°ó¶¨µÄÔªËØÉÏʱº¯Êý²Å»áÔËÐС£¶øµ½´ËʱΪֹ£¬ÎÒÃǵÄÆäËûµÄÀ´×Ô.bind()µÄ´¦Àíº¯ÊýÔçÒÑÔËÐÐÁË¡£
ÍƼöÐÅÏ¢
- jqueryʵÏÖÍøվͼƬÀÁ¼ÓÔØ´úÂë
- jQuery Tooltips²å¼þ
- jQueryµÄ.bind()¡¢.live()ºÍ.delegate(....
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- Ä㲻ϲ»¶»ðºü£¬µ«ÓÖϲ»¶firebug£¬ÄǾ͸úÎÒÒ»ÆðÀ´ÔÚIEÉÏ....
- ¸ù¾ÝÏÔʾÆ÷·Ö±æÂʵ÷ÕûÒ³ÃæÏÔʾµÄJavascript½Å±¾
- jQuery дµÄ·ÂÐÂÀË΢²© ÏòϹö¶¯Ð§¹û¡¾×ªÔØ¡¿
- ͨÓõÄJqueryÑ¡ÔñÆ÷
- jQuery ½áºÏ Json Ìá½»Êý¾Ýµ½Webservice£¬....
- ×ÔÖÆjQueryÖÇÄÜÌáʾ²å¼þһö
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- js»ñÈ¡srcÖд«µÝµÄ²ÎÊý
- jqueryʵÏÖÍøվͼƬÀÁ¼ÓÔØ´úÂë
- ÈÃä¯ÀÀÆ÷ÆÁ±ÎµôJavaScriptµÄ³ö´íÌáʾ
- github²»ÄܼÓÔØcss¡¢js½â¾ö°ì·¨
- ÎÒÖªµÀµÄJavaScript -- Éè¼Æģʽ(ÇŽÓ)Ó¦ÓÃÖ® ¨C ....
- 15¸öÖµµÃ¿ª·¢ÈËÔ±¹Ø×¢µÄjQuery¿ª·¢¼¼ÇɺÍÐĵÃ
- dz̸JavascriptÃæÏò¶ÔÏó±à³Ì
- JS¼òµ¥¶¯»·â×°
- JavaScript³õѧÕßӦעÒâµÄÆ߸öϸ½Ú
- jQuery Tooltips²å¼þ
ÆÀÂÛ