// This will create a red background on every element on the page with a CLASS of // "redBack". Notice that it doesn't matter which HTML element this "redBack" // CLASS tag is attached to. Also notice the period in the front of the query // term -- this is the jQuery syntax for finding the CLASS names. $(".redBack").css("background", "#ff0000"); <p>This is a paragraph</p> <div>This is a big div</div> <table><tr><td>Sample table</td></tr></table>
ºÏ²¢ËÑË÷Ìõ¼þ
¿ÉÒÔÔÚÒ»¸öËÑË÷ÖУ¬½«ÒÔÉ쵀 3 ¸öËÑË÷Ìõ¼þºÍÏÂÃæµÄËùÓйýÂËÆ÷ºÏ²¢ÆðÀ´¡£Í¨¹ýʹÓà ¡°,¡± ·Ö¸ôÿ¸öËÑË÷Ìõ¼þ£¬ËÑË÷½«·µ»ØÓëËÑË÷´ÊÆ¥ÅäµÄÒ»×é½á¹û¡£
Çåµ¥ 8. ºÏ²¢ËÑË÷
// This will hide every <p>, <span>, or <div>. $("p, span, div").hide();
¸ü¶àµÄ¹ýÂËÆ÷
ËäÈ»ÔÚ jQuery ÖУ¬Õâ 3 ¸öËÑË÷²ÎÊýÎÞÒÉÊÇ×î³£Óõģ¬µ«»¹ÓÐÐí¶àÆäËûËÑË÷²ÎÊý£¬¿ÉÒÔ°ïÖúÄúÔÚÒ»¸öÒ³ÃæÉÏ¿ìËÙ²éÕÒËùÐèµÄÔªËØ¡£ÕâЩ¹ýÂËÆ÷ÒÔ ¡°:¡± ¿ªÍ·£¬±íÃ÷ËüÃÇÊÇ jQuery ËÑË÷´ÊÖеĹýÂËÆ÷¡£¾¡¹ÜËüÃÇÒ²¿ÉÒÔ×÷Ϊ¶ÀÁ¢µÄËÑË÷Ìõ¼þ£¬µ«ÊÇÉè¼ÆËüÃǵÄÄ¿µÄÊǽ«ËüÃǺÍÒÔÉÏ 3 ¸öËÑË÷Ìõ¼þÒ»ÆðʹÓ㬴Ӷø¿ÉÒÔµ÷ÕûËÑË÷Ìõ¼þÒÔÕÒµ½ÄúÐèÒªµÄÌض¨ÔªËØ¡£
Çåµ¥ 9. ¸ü¶àµÄ¹ýÂËÆ÷
// This will hide every <p> tag on a page $("p").hide(); // This will hide the first element on a page, no matter its HTML tag $(":first").hide(); // Notice how these can be used in combination to provide more fine tuning of // search criteria. This will hide only the first <p> tag on a given page. $("p:first").hide();
¿ÉÒÔ½«¶à¸ö¹ýÂËÆ÷ÓÃ×÷ËÑË÷ÔªËØ¡£ËäÈ»ÔÚÕâÀïÎÒûÓÐÁоÙËùÓеĹýÂËÆ÷£¨ÕâÊÇ API Ò³ÃæµÄÈÎÎñ£©£¬µ«ÆäÖÐһЩ¹ýÂËÆ÷ÔÚ´¦ÀíÒ³ÃæºÍËÑË÷ÔªËØ·½Ãæ·Ç³£·½±ã¡£
ÎÒ½«Ö÷Òª¹Ø×¢ Selection °üÖÐһЩ·Ç³£ÖØÒªµÄ¹ýÂËÆ÷£¬ËüÃǾÍÊÇ±íµ¥ÔªËعýÂËÆ÷¡£Èç½ñµÄ¸» Internet Ó¦ÓóÌÐò±È½Ï¹Ø×¢±íµ¥¼°°üº¬ÔÚÆäÄÚµÄÔªËØ£¨Îı¾×ֶΡ¢°´Å¥¡¢¸´Ñ¡¿ò¡¢µ¥Ñ¡°´Å¥µÈ£©£¬ËüÃÇ´Ó·þÎñÆ÷ÊÕ¼¯ºÍ´«ÊäÐÅÏ¢£¬»òÊÕ¼¯ÐÅÏ¢²¢´«Êäµ½·þÎñÆ÷¡£ÓÉÓÚËüÃÇÔÚ RIA ÖеÄÖØÒª×÷Óã¬ÔÚµ±½ñµÄ Web Ó¦ÓóÌÐòÖУ¬ÕâЩ¹ýÂËÆ÷ÔÚ´¦Àí jQuery ʱ·Ç³£ÖØÒª¡£
ÕâЩ¹ýÂËÆ÷ºÍÇ°Ãæ½éÉܵĹýÂËÆ÷µÄ¹¤×÷ÔÀíÊÇÒ»ÑùµÄ£¬²¢ÇÒÒ²ÊÇÒÔ ¡°:¡± ¿ªÍ·£¬±íÃ÷ËüÃÇÊǹýÂËÆ÷¡£Í¬Ñù£¬ËüÃÇÒ²¿ÉÒÔºÍÆäËûËÑË÷¹ýÂËÆ÷Ò»ÆðʹÓã¬ÒÔϸ»¯ËÑË÷Ìõ¼þ¡£Òò´Ë£¬Ò»¸ö ¡°:text¡± ËÑË÷¹ýÂËÆ÷½«·µ»ØÒ³ÃæÉϵÄÿ¸öÎı¾×ֶΣ¬¶øÒ»¸ö ¡°.largeFont:text¡± ËÑË÷¹ýÂËÆ÷½ö·µ»ØÒ³ÃæÉÏ×÷Ϊ ¡°largeFont¡± ÀàµÄÒ»²¿·ÖµÄÎı¾×ֶΡ£ÕâÔÊÐí½øÒ»²½Ï¸»¯ºÍ²Ù×÷±íµ¥ÔªËØ¡£
±íµ¥¹ýÂËÆ÷Ò²°üÀ¨ÔªËصÄÿ¸öÊôÐÔ£¬Á˽âÕâ·½ÃæµÄ֪ʶ¶Ô¿ª·¢ÈËÔ±Óкô¦¡£Òò´ËÏñ ¡°:checked¡±¡¢¡°:disabled¡± ºÍ ¡°:selected¡± µÈËÑË÷¹ýÂËÆ÷½«ÎªÌض¨µÄËÑË÷½øÒ»²½Ï¸»¯ËÑË÷Ìõ¼þ¡£
±éÀú
ÏÖÔÚ£¬ÄúÒѾѧ»áÈçºÎËÑË÷ºÍ¹ýÂËÒ³ÃæÉϵÄËùÓÐÔªËØ£¬½ÓÏÂÀ´ÐèÒªÒ»ÖÖ¸ßЧµÄ·½·¨À´±éÀú½á¹û£¬½øÒ»²½´¦ÀíÔªËØ¡£×ÔÈ»£¬jQuery ÌṩÁ˼¸ÖÖ±éÀúËÑË÷½á¹ûµÄ·½·¨¡£
µÚÒ»¸öÒ²ÊÇ×î³£ÓõıéÀú·½·¨ÊÇ each() º¯Êý¡£ÕâºÍ ¡°for loop¡± µÄ¹¦ÄÜÊÇÒ»ÑùµÄ£¬±éÀúÿ¸öÔªËز¢Í¨¹ýµü´úµÝÔöÔªËØ¡£´ËÍ⣬ѻ·ÖеÄÿ¸öÔªËصÄÒýÓÿÉÒÔͨ¹ý ¡°this¡±£¨ÓÃÓÚÒ»°ãµÄ JavaScript Óï·¨£©»ò $(this)£¨ÓÃÓÚ jQuery ÃüÁÀ´ÊµÏÖ¡£
ÈÃÎÒÃÇ¿´¿´ÏÂÃæµÄʾÀý¡£
Çåµ¥ 10. each Ñ»·
// Will loop through each <p> tag on the page. Notice the use of the // inline function here -- this is analogous with the anonymous classes in Java. // You can either call a separate function, or write an inline function like this. var increment = 1; $("p").each(function(){ // now add a paragraph count in front of each of them. Notice how we use the // $(this) variable to reference each of the paragraph elements individually. $(this).text(increment + ". " + $(this).text()); increment++; });
ÒòΪËÑË÷½á¹û´æ´¢ÔÚÒ»¸öÊý×éÖУ¬Äú¿Ï¶¨Ï£Íûº¯Êý±éÀú¸ÃÊý×飬¾ÍÏñ´¦ÀíÆäËû±à³ÌÓïÑÔµÄÊý¾Ý¶ÔÏóÒ»Ñù¡£Òò´Ë£¬Òª²éÕÒÒ»¸ö¸ø¶¨ËÑË÷½á¹ûµÄ³¤¶È£¬Ôò¿ÉÒÔÔÚ¸ÃÊý×éÉϵ÷Óà $().length¡£Çåµ¥ 11 չʾÁ˸ü¶àµÄÊý×é±éÀúº¯Êý£¬¿ÉÊÊÓÃÓÚÆäËû±à³ÌÓïÑÔµÄÊý×é±éÀú¡£
Çåµ¥ 11. ÆäËûÊý×麯Êý
// the eq() function lets you reference an element in the array directly. // In this case, it will get the 3rd paragraph (0 referenced of course) and hide it $("p").eq(2).hide(); // The slice() function lets you input a start and an end index in the array, to // create a subset of the array. This will hide the 3rd through 5th paragraphs on the // page $("p").slice(2,5).hide();
³ýÁËÕâЩÊý×é±éÀúº¯ÊýÖ®Í⣬jQuery »¹ÌṩÁËһЩº¯Êý£¬Ê¹Äú¿ÉÒÔ²éÕÒǶÌ×ÔÚËÑË÷´ÊÖÜΧµÄÔªËØ¡£ÎªÊ²Ã´ÕâºÜÓÐÓÃÄØ£¿ÀýÈ磬ÎÒÃdz£³£ÐèÒªÔÚͼƬµÄÅÔ±ßǶÈëÒ»¸öÎı¾±êÇ©£¬»òÔÚ±íµ¥ÔªËØÅÔ±ßǶÈëÒ»¸ö´íÎóÏûÏ¢¡£Ê¹ÓÃÕâЩÃüÁî¿ÉÒÔ²éÕÒÌض¨µÄ±íµ¥ÔªËØ£¬È»ºóͨ¹ý½«±íµ¥ÔªËØ·ÅÖÃÔÚÏÂÒ»¸öÔªËØ£¨span ±ê¼Ç£©ÖУ¬°Ñ¸Ã´íÎóÏûÏ¢Ö±½Ó·ÅÖÃÔÚ±íµ¥ÔªËØÅԱߡ£Çåµ¥ 12 ÏÔʾÁËÕâÖÖÉè¼ÆµÄÒ»¸öʾÀý£º
Çåµ¥ 12. ʾÀý next() º¯Êý
<input type=text class=validate><span></span> function validateForm() { $(".validate:text").each(function(){ if ($(this).val()=="") // We'll loop through each textfield on the page with a class of "validate" // and if they are blank, we will put text in the <span> immediately afterwards // with the error message. $(this).next().html("This field cannot be blank"); }); }
×ÛºÏѧµ½µÄ֪ʶ
ÒªÁ˽âÈçºÎ½áºÏʹÓÃÒÔÉÏ֪ʶ£¬¿ÉÒԲ鿴±¾ÎÄ°üº¬µÄʾÀýÓ¦ÓóÌÐò£¨²Î¼ûС½Ú£©¡£
¡¡