jQuery 1.4.2ÖеÄ.bind(),.live()&.delegate()·½·¨½âÎö
.bind£¨£©·½·¨
.bind£¨£©·½·¨Äܹ»½«Ò»¸ö²Ù×÷°ó¶¨µ½Ìض¨µÄÔªËØÉÏÈ¥£¬È»ºóÔÚʼþ±»µ÷ÓõÄʱºò½øÐд¥·¢¡£ÐèҪעÒâµÄÊÇ.bind()·½·¨Ö»Äܹ»±»°ó¶¨µ½ÒѾ´æÔÚ µÄÔªËØÉÏÈ¥£¬²ÅÄܹ»±»Ö´ÐС£Èç¹ûÁíÒ»¸ö·ûºÏÒªÇóµÄÔªËØÔÚ.bind()ʼþ°ó¶¨Ö®ºó£¬²Å±»Ìí¼Óµ½dom½á¹¹ÖУ¬ÄÇôÕâ¸öÔªËؾͲ»»á±»´¥·¢¡£
ʾÀýÈçÏ£º
jQuery: $('.alertMe', '#bindExample').bind('click', function() { alert('Clicked!'); }); $('#bindExample').append('<p class="alertMe">Alert <em>does not</em> fire on click!</p>'); HTML: <div id="bindExample"> <!-- action is bound to this paragraph for the click event --> <p class="alertMe">Alert fires on click!</p> </div>
µ«ÊÇ£¬Õâ¸öʾÀýÀïÃ棬ÐÂÌí¼Ó½øÈ¥µÄÔªËØÊDz»Äܹ»±»´¥·¢µÄ¡£
.live()·½·¨
ºÍ.bind()·½·¨ÀàËÆ£¬.live()·½·¨Ò²Äܹ»½«Ê¼þ°ó¶¨µ½ÔªËØÉÏÃ档Ȼ¶ø£¬ÓëÖ®²»Í¬µÄÊÇ,.live()·½·¨Äܹ»½«Óë֮ƥÅäµÄÔªËؽøÐÐʼþ°ó¶¨£¬¼´±ãÊÇÔªËر»¶¯Ì¬µÄ´´½¨£¬²¢ÇÒÔÚ.live()·½·¨µ÷ÓÃÖ®ºó±»´´½¨¡£
ÐèҪעÒâµÄÊÇ£¬dom±éÀú·½·¨²»ÍêÈ«Ö§³Ö´«Í³µÄjQueryÔªËصÄËÑË÷·½·¨£¬±ÈÈ磬²»Ö§³Ö
$('#foo').children('.bar').live('click',function(){//do something });
µ«ÊÇÈ´Ö§³Ö£º
$('#foo .bar').live('click', function() { /do something });
ËùÒÔ£¬×îºÃµÄ·½·¨¾ÍÊÇÖ±½Ó°ó¶¨ÔªËØ£¬µ±È».find()·½·¨Ò²ÊÇ¿ÉÒÔ±»Ö§³ÖµÄ¡£
ʾÀýÈçÏ£º
jQuery: $('.alertMe', '#liveExample').live('click', function() { alert('Clicked!');});$('#liveExample').append('<p class="alertMe">Alert fires on click (even though this was added after .live() was called)!</p>');HTML: <!-- action is bound to this parent div for child paragraph's click event --><div id="liveExample"> <!-- action is not bound to this paragraph --> <p class="alertMe">Alert fires on click!</p></div>
Õâ´Î£¬°ó¶¨Ê¼þ¾Í»á±»´¥·¢ÁË¡£
.delegate()
·½·¨
.delegate()
·½·¨Í¬
live
·½·¨Ò»Ñù£¬Ò²ÊÇ°ó¶¨Ê¼þµÄ£¬µ«ÊÇÆäЧÂÊÒª±È
live
·½·¨ºÃÉÏ
4
±¶²»Ö¹£¬²¢ÇÒËûÖ»°ó¶¨Ñ¡ÔñÆ÷ÖÐÖ¸¶¨µÄÔªËØ£¬¶ø²»ÊÇ´Ó¸ùÔªËذ󶨿ªÊ¼¡£ËùÒÔ¶ÔÓÚÀàËÆÓÚ
$('#foo').delegate('.bar', ...)µÄ°ó¶¨À´Ëµ£¬.barÉÏÃæ°ó¶¨µÄʼþ½«»á±»Í¬Ê±°ó¶¨µ½#fooÔªËØÉÏ¡£²»¹ý£¬ÐèҪСÐĵÄÊÇ£¬Èç¹ûÑ¡ÔñÆ÷±¾ÉíÊDZ»ajax¸ü»»µÄ£¬ÄÇô·ûºÏÌõ¼þµÄÌæ»»ÔªËؽ«²»»á±»°ó¶¨¡£
.delegate()²»»á±»domÉú³ÉµÄÔªËØËùÏÞÖÆ£¬µ«ÊÇ.live()·½·¨»á¡£
ʾÀýÈçÏ£ºjQuery: $('#delegateExample').delegate('.alertMe', 'click', function() { alert('Clicked!');});$('#delegateExample').append('<p class="alertMe">Alert fires on click (even though this was added after .delegate() was called)!</p>');HTML: <!-- action is bound to this parent div for child paragraph's click event --><div id="delegateExample"> <!-- action is not bound to this paragraph --> <p class="alertMe">Alert fires on click!</p></div>
ÍƼöÐÅÏ¢
- 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²å¼þ
ÆÀÂÛ