±¾ÎÄÌṩ¼´¿ÌÌáÉýÄãµÄ½Å±¾ÐÔÄܵÄÊ®¸ö²½Öè¡£²»Óõ£ÐÄ£¬Õâ²¢²»ÊÇʲô¸ßÉîµÄ¼¼ÇÉ¡£ÈËÈ˽ԿÉÔËÓã¡ÕâЩ¼¼ÇÉ°üÀ¨£º
ʹÓÃ×îа汾jQueryÒ»Ö±´¦ÓÚ²»¶ÏµÄ¿ª·¢ºÍ¸Ä½ø¹ý³ÌÖС£ John ºÍËûµÄÍŶӲ»¶ÏÑо¿×ÅÌáÉý³ÌÐòÐÔÄܵÄз½·¨¡£
Ò»µãÌâÍâ»°£¬¼¸¸öÔÂÇ°Ëû»¹·¢²¼ÁËSizzle£¬Ò»¸ö¾Ý˵ÄÜÔÚFirefoxÖаѳÌÐòÐÔÄÜÌáÉý3±¶µÄJSÑ¡ÔñÆ÷¿â¡£
Èç¹ûÄã²»Ïëʱ¿Ì¹Ø×¢ÊÇ·ñÓÐа汾£¬È»ºóÔÙ»¨Ê±¼äÏÂÔØÉÏ´«£¬Google ¾ÍÓÖÄÜ°ïÄãÒ»°ÑÁË¡£ËûÃǵķþÎñÆ÷ÉÏ´æ´¢ÁË´óÁ¿Ajax¿â¹©ÄúÑ¡Ôñ¡£
<!-- ÀûÓÃÒ»¸ö¼òµ¥µÄscript±êÇ©µ÷ÓÃAPI --> <script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript"> /* ¼ÓÔØ jQuery v1.3.2 */ google.load ("jquery", "1.3.2", {uncompressed: false}); /* ¼ÓÔØÍê³Éºóµ¯³öÏûÏ¢ */ function onLoad () { alert ("jQuery + Google API!"); } google.setOnLoadCallback (onLoad); </script>ÁíÒ»¸ö¸üΪ¼òµ¥¿ìËٵķ½·¨ÊÇÖ±½ÓʹÓýű¾Á´½Ó¡£Èç¹ûҪʹÓÃÌض¨°æ±¾µÄjQuery£¬Äã¿ÉÒÔʹÓÃÉÏÃæµÄ·½·¨£»Èç¹ûÏëÖ±½ÓʹÓÃ×îа棬ÏÂÃæÕâ¾ä´úÂë¾Í¹»ÁË£º
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>Ìض¨°æ±¾µÄ»¹¿ÉÒÔÕâÑù¼ÓÔØ£º
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> ºÏ²¢¡¢×îС»¯½Å±¾´ó²¿·Öä¯ÀÀÆ÷¶¼²»ÄÜͬʱ´¦Àí¶à¸ö½Å±¾Îļþ£¬ËùÒÔËüÃǶ¼ÊÇÅŶӼÓÔØ¡ª¡ª¼ÓÔØʱ¼äÒ²ÏàÓ¦µØÑÓ³¤ÁË¡£
¿¼Âǵ½ÄãÍøÕ¾µÄÿ¸öÒ³Ã涼»á¼ÓÔØÕâЩ½Å±¾£¬ÄãÓ¦¸Ã¿¼ÂÇ°ÑËüÃǷŵ½µ¥¸öÎļþÖУ¬È»ºóÀûÓÃѹËõ¹¤¾ß£¨±ÈÈç Dean Edwards µÄÕâ¿î£©°ÑËüÃÇ×îС»¯¡£¸üСµÄÎļþÎÞÒɽ«´øÀ´¸ü¿ìµÄ¼ÓÔØËٶȡ£
JavaScriptºÍCSSѹËõµÄÄ¿µÄÊÇÔÚ±£³Ö½Å±¾µÄÖ´ÐÐÐÔÄܵÄͬʱ£¬¼õÉÙÊý¾Ý´«µÝµÄ×Ö½ÚÊý£¨¿ÉÒÔͨ¹ý¼õСÔʼÎļþ£¬Ò²¿ÉÒÔÀûÓÃgzip¡£´ó¶àÊý²úÆ·¼¶µÄÍøÂç·þÎñÆ÷¶¼°Ñgzip×÷ΪHTTPÐÒéµÄÒ»²¿·Ö£©¡£Òý×Ô YUI compressor£¬Ò»¿î jQuery¹Ù·½ÍƼöµÄѹËõ½Å±¾µÄ¹¤¾ß¡£
ÓÃforÌæ´úeachÔÉúº¯Êý×ÜÊDZȸ¨Öú×é¼þ¸ü¿ì¡£
Èç¹ûÓöµ½ÐèÒª±éÀú¶ÔÏóµÄÇé¿ö£¨Èç´ÓÔ¶³Ì½ÓÊÕµÄJSON¶ÔÏ󣩣¬Äã×îºÃÖØдÄãµÄ£¨JSON£©¶ÔÏóΪһ¸öÊý×飬Êý×éµÄÑ»·´¦ÀíÒªÈÝÒ×Щ¡£
ÀûÓÃFirebug£¬ÎÒÃÇÄܲⶨÿ¸öº¯ÊýµÄÖ´ÐÐʱ¼ä¡£
var array = new Array (); for (var i=0; i<10000; i++) { array[i] = 0; } console.time('native'); //ÔÉúforº¯Êý var l = array.length; for (var i=0; i<10000; i++) { }ÉÏÃæµÄ½á¹ûÏÔʾÔÉú´úÂëÖ»Ðè2ºÁÃë¾Í×öµ½µÄÊ£¬ÀûÓÃjQueryµÄeach·½·¨ÐèÒª26ºÁÃë¡£¶øÇÒÕ⻹ֻÊÇÎÒÔÚ±¾»úÉϲâÊÔÒ»¸ö»ù±¾ÉÏɶҲû×öµÄº¯ÊýµÄ½á¹û£¬µ±Óöµ½¸ü¸´ÔÓµÄÇé¿ö£¬ÀýÈçÉèÖÃcssÊôÐÔ»òDOM²Ù×÷ʱ£¬Ê±¼ä²îÒì¿Ï¶¨¸ü´ó¡£
ÓÃIDÌæ´úclassÑ¡ÔñÆ÷ÀûÓÃIDÑ¡Ôñ¶ÔÏóÒªºÃµÃ¶à£¬ÒòΪÕâʱjQuery»áʹÓÃä¯ÀÀÆ÷µÄÔÉúº¯ÊýgetElementByID()À´»ñÈ¡¶ÔÏ󣬲éѯËٶȺܿ졣
Òò´Ë£¬±ÈÆðÀûÓÃÄÇЩ·½±ãµÄcssÑ¡Ôñ¼¼ÇÉ£¬Ê¹ÓøüΪ¸´ÔÓµÄÑ¡ÔñÆ÷Ò²ÊÇÖµµÃµÄ£¨jQueryҲΪÎÒÃÇÌṩÁ˸´ÔÓÑ¡ÔñÆ÷£©¡£ÄãÒ²¿ÉÒÔÊÖ¹¤Êéд×Ô¼ºµÄÑ¡ÔñÆ÷£¨Æäʵ±ÈÄãÏëÏóÖмòµ¥£©£¬»òÕßΪÄãÏëҪѡÔñµÄÔªËØÖ¸¶¨Ò»¸öÓÐIDµÄÈÝÆ÷¡£
//ÏÂÀý´´½¨Ò»¸öÁÐ±í²¢ÇÒÌî³äÌõÄ¿ÄÚÈÝ //È»ºóÿ¸öÌõÄ¿¶¼±»Ñ¡ÔñÒ»´Î //Ê×ÏÈʹÓÃclassÑ¡Ôñ console.time('class'); var list = $('#list'); var items = ' '; for (i=0; i<1000; i++) { items += ' item '; } items += ' '; list.html (items); for (i=0; i<1000; i++) { var s = $('.item' + i); } console.timeEnd('class'); //È»ºóÀûÓÃIDÑ¡Ôñ console.time('id'); var list = $('#list'); var items = ' '; for (i=0; i<1000; i++) { items += ' item '; } items += ' '; list.html (items); for (i=0; i<1000; i++) { var s = $('#item' + i); } console.timeEnd('id');ÉÏÃæµÄÀý×ӺܺõØ˵Ã÷Á˲»Í¬Ñ¡Ôñ·½Ê½Ö®¼äµÄÏÔÖøÐÔÄܲîÒì¡£Çë¿´ÏÂͼ£¬ÀûÓÃclassÀ´×öÑ¡Ôñ£¬Ê±¼äÎÞÏÞÔö´ó£¬ÉõÖÁ³¬¹ýÁËÎåÃë¡£
¸øÑ¡ÔñÆ÷Ö¸¶¨Ç°ºóÎÄjQueryµÄ²Î¿¼ÎĵµÀï˵£º´«µÝ¸øjQuery() ÔʼDOM½ÚµãµÄÇ°ºóÎÄ£¨Èç¹ûûÓж«Î÷±»´«µÝ£¬ÔòÇ°ºóÎÄΪÕû¸öÎĵµ£©¡£Ä¿µÄÊÇÁ¬Í¬Ñ¡ÔñÆ÷Ò»Æð£¬ÊµÏÖ¸üΪ׼ȷµÄ²éѯ¡£
ËùÒÔ£¬Èç¹ûÄãÒ»¶¨ÒªÀûÓÃclassÀ´Ö¸¶¨Ä¿±ê£¬ÖÁÉÙΪѡÔñÆ÷Ö¸¶¨ÉÏÏÂÎÄ£¬ÒÔÃâjQuery·Ñ¾«Á¦È¥±éÀúÕû¸öDOMÎĵµ£º
ÓëÆäÕâÑùд£º
$('.class').css ('color' '#123456');ΪѡÔñÆ÷¼ÓÉÏÇ°ºóÎıȽϺã¨expression: Ä¿±êÑ¡ÔñÆ÷£»context: Ç°ºóÎÄ£©£º
$(expression, context)Ò²¾ÍÊÇ˵£º
$('.class', '#class-container').css ('color', '#123456');ÕâÑù×öÒª¿ìµÃ¶à£¬ÒòΪËü²»ÓñéÀúÕû¸öDOM¡£Ö»ÒªÕÒµ½#class-container¾ÍºÃÁË¡£
½¨Á¢»º´æ²»Òª·¸²»¶ÏÖØÐÂÑ¡Ôñͬһ¸ö¶«Î÷µÄ´íÎó¡£ÄãÓ¦¸Ã°ÑÄãÒª´¦ÀíµÄÔªËØ»º´æΪһ¸ö±äÁ¿¡£
¸ü²»ÒªÔÚÒ»¸öÑ»·ÀïÖظ´Ñ¡Ôñͬһ¸öÔªËØ£¡ÕâÑù×öÊ®·ÖÓ°ÏìËٶȣ¡
$('#item').css('color', '#123456'); $('#item').html('hello'); $('#item').css('background-color', '#ffffff'); // ÕâÑùд¸üºÃ $('#item').css('color', '#123456').html('hello').css('background-color', '#ffffff'); // ÉõÖÁÕâÑù var item = $('#item'); item.css('color', '#123456'); item.html('hello'); item.css('background-color', '#ffffff'); // Óöµ½Ñ»·,ÕâÑù×ö·Ç³£²»ºÃ console.time('no cache'); for (var i=0; i<1000; i++) { $('#list').append(i); } console.timeEnd('no cache'); // ÏÂÃæÕâÑùÒªºÃµÃ¶à console.time('cache'); var item = $('#list'); for (var i=0; i<1000; i++) { item.append (i); } console.timeEnd('cache'); ±ÜÃâDOM²Ù×÷DOM²Ù×÷Ó¦¸ÃÔ½ÉÙÔ½ºÃ£¬ÒòΪÖîÈçprepend()£¬append()£¬after()µÄ²åÈ붯×÷¶¼ºÜ·Ñʱ¡£ÉÏÃæµÄÀý×ÓÈç¹ûÓÃhtml()»á¸ü¿ì£º
var list = ''; for (var i=0; i<1000; i++) { list += ' '+i+' '; } ('#list').html (list); ±ÜÃâʹÓÃconcat()£¬ÀûÓÃjoin()´¦Àí³¤×Ö´®ÌýÆðÀ´¿ÉÄÜͦÆæ¹Ö£¬²»¹ýÕâÑù×öÕæµÄÄÜÌáÉýËٶȣ¬ÓÈÆäÊǵ±Á¬½ÓÌر𳤵Ä×Ö´®Ê±¡£ÏȽ¨Á¢Ò»¸öÊý×飬·ÅÈëÄãÏëÒª´®ÁªµÄ¶«Î÷¡£join()·½·¨±È×Ö·û´®µÄconcat()º¯ÊýÒª¿ìµÃ¶à¡£
var array = []; for (var i=0; i< =10000; i++) { array[i] = ' '+i+''; } $('#list').html(array.join (''));
¡¡