jQueryÈç½ñÒѾ³ÉΪWeb¿ª·¢ÖÐ×îÁ÷ÐеÄJavaScript¿â£¬Í¨¹ýjQueryºÍ´óÁ¿µÄ²å¼þ£¬Äã¿ÉÒÔÇáËÉʵÏÖ¸÷ÖÖѤÀöµÄЧ¹û¡£
±¾ÎĽ«ÎªÄã½éÉÜһЩʵÓõļ¼ÇÉ£¬Ï£Íû¿ÉÒÔ°ïÖúÄã¸ü¼Ó¸ßЧµØʹÓÃjQuery¡£
Tip 1£ºÊ¹ÓÃ×îа汾
jQueryÿһ¸öа汾¶¼»á°üº¬Ò»Ð©ÐÔÄÜÓÅ»¯ºÍbugÐÞ¸´£¬ÎªÁ˱ãÓÚÉý¼¶£¬Äã¿ÉÒÔʹÓÃGoogle CDN·þÎñÍйܵÄjQuery¿â¡£ÓÐÒÔÏÂÁ½ÖÖ·½Ê½£º
°üº¬Ìض¨°æ±¾
Html´úÂë
<script src=""></script>
°üº¬Ä³¸ö·ÖÖ§µÄµÄ×îа汾£¨´Ë·½·¨ÖÐjQuery°æ±¾µÄ»º´æÆÚÏÞÖ»ÓÐ1Сʱ£¬²»½¨ÒéÓÃÓÚÉú²ú»·¾³£©
Html´úÂë
<script src=""></script>
Tip 2£ºÊ¹Óüòµ¥µÄÑ¡ÔñÆ÷
֮ǰ»ñÈ¡DOMÔªËØͨ³£Ê¹ÓÃjQueryµÄgetElementById()¡¢getElementsByTagName()ºÍgetElementsByClassName()·½·¨¡£Èç½ñ£¬ËùÓÐÖ÷Á÷ä¯ÀÀÆ÷¶¼ÒѾ֧³ÖquerySelectorAll()£¬¸Ã·½·¨Äܹ»Àí½âCSS²éѯÆ÷¡£ÄãÓ¦¸Ã³¢ÊÔʹÓÃÕâ¸ö¸üÓŵķ½Ê½¡£
Javascript´úÂë
$('li[data-selected="true"] a') // ¿´ÆðÀ´²»´í£¬µ«ÊÇÂý
$('li.selected a') // ¸üºÃµÄ·½·¨
$('#ElementID) // ×îºÃ
Tip 3£º»º´æjQueryµÄ½á¹û
Èç¹ûÄãûÓбðµÄÑ¡Ôñ£¬Ö»ÄÜʹÓÃDOMÑ¡ÔñÆ÷£¬ÄÇôÄãÓ¦¸Ã»º´æjQueryµÄ½á¹û¡£ÀýÈ磺
Javascript´úÂë
var selectedListItem = $('li[data-selected="true"]a')
ÏÖÔÚ£¬jQueryµÄ½á¹ûÒѾ±»»º´æµ½±äÁ¿“selectedListItem”£¬¸Ã±äÁ¿¿ÉÒÔ¶à´ÎʹÓöø²»»áÓ°ÏìÐÔÄÜ¡£
Tip 4£ºÊ¹ÓÃjQueryÀ©Õ¹Ñ¡ÔñÆ÷µÄ×¢ÒâÊÂÏî
jQueryÌṩÁË´óÁ¿µÄÀ©Õ¹Ñ¡ÔñÆ÷£¬±ÈÈç:visible¡¢:hidden¡¢:animatedµÈ£¬¶øËüÃDz»ÊÇÓÐЧµÄCSS3Ñ¡ÔñÆ÷¡£Èç¹ûʹÓÃÕâЩѡÔñÆ÷£¬½«²»ÄÜÔÙʹÓÃquerySelectorAll()·½·¨¡£ÎªÁ˱ÜÃâÕâÖÖÇé¿ö£¬Äã¿ÉÒÔÏÈÑ¡ÔñÔªËØ£¬È»ºóÔÙ¹ýÂË¡£ÀýÈ磺
Javascript´úÂë
$('a.button:hidden'); //Ôò²»ÄÜʹÓÃquerySelectorAll()
$('a.button').filter(':hidden'); //×î¼Ñ·½°¸
ÉÏÊö½á¹ûÊÇÏàͬµÄ£¬µ«ÊǵÚ2¸ö¸ü¿ì¡£
Tip 5£ºÏñÊý×éÒ»ÑùʹÓÃjQuery¶ÔÏó
ÔËÐÐÒ»¸öÑ¡ÔñÆ÷µÃµ½µÄ½á¹ûÊÇÒ»¸öjQuery¶ÔÏó¡£µ«ÊÇ£¬Í¨¹ýjQuery¿ÉÒÔʹ½á¹û¿´ÆðÀ´¸üÏñÒ»¸öÊý×飬Äã¿ÉÒÔ¶¨ÒåË÷ÒýÔªËغͳ¤¶È¡£
Javascript´úÂë
var buttons = $('#navigation a.button'); //Selecting all the navigation b //Selecting all the navigation buttons
// We can loop though the collection:
for(var i=0;i<buttons.length;i++){
console.log(buttons[i]); // A DOM element, not a jQuery object
}
Èç¹ûÄãÏëʵÏÖ¸ü¸ßµÄÐÔÄÜ£¬¿ÉÒÔʹÓÃÒ»¸ö¼òµ¥µÄÑ»·£¨»òwhileÓï¾ä£©À´´úÌæ$.each()£¬ÕâÑùËٶȻá±È֮ǰ¿ì¼¸±¶¡£
Tip 6£º¼ì²éÒ»¸öÔªËØÊÇ·ñ´æÔÚ
È·¶¨Ò»¸öÔªËؼ¯ºÏÊÇ·ñ´æÔÚ»òÊÇ·ñ°üº¬ÔªËصÄΨһ·½·¨ÊǼì²éÔªËصij¤¶È¡£
Javascript´úÂë
If (buttons.length){ // True only if buttons contains elements
// Do something }
Tip 7£º´´½¨jQuery¿Õ¶ÔÏó
´´½¨Ò»¸öеÄjQuery¶ÔÏóÓÐʱ¿ªÏú»á±È½Ï´ó¡£²»¹ýÄã¿ÉÒÔÏÈ´´½¨Ò»¸ö¿Õ¶ÔÏó£¬È»ºóͨ¹ýadd()Ìî³äËü¡£
Javascript´úÂë
var container = $([]);
container.add(another_element);
Tip 8£ºÍ³¼ÆWebÒ³ÃæÖÐDOMÔªËØÊý
Èç¹ûÒ³ÃæÖаüº¬´óÁ¿ÔªËØ»òÄÚÈÝ£¬Ôòä¯ÀÀÆ÷äÖȾËùÐèµÄʱ¼äÒ²¸ü¶à¡£Äã¿ÉÒÔÔÚ¿ØÖÆ̨ÖÐÖ´ÐÐÒÔÏÂÓï¾ä£¬À´Í³¼ÆÒ³ÃæÖеÄDOMÔªËØÊý£º
Javascript´úÂë
console.log($('*').length);
Èç¹ûËùµÃÊýÖµ½ÏС£¬ÔòÒ³ÃæäÖȾ½Ï¿ì¡£Äã¿ÉÒÔͨ¹ýɾ³ý¶àÓàµÄ±ê¼ÇºÍ²»±ØÒªµÄÔªËØÀ´½øÐÐÓÅ»¯¡£
Tip 9£º°ÑÄãµÄ´úÂë±ä³ÉjQuery²å¼þ
Èç¹ûÄãÏ£Íû½«ÄãµÄjQuery´úÂë·â×°³ÉÒ»¸öjQuery²å¼þ£¬ÒÔ±ãÒÔºóÖØÓã¬Äã¿ÉÒÔͨ¹ýÒÔÏ´úÂëÀ´´´½¨£º
Javascript´úÂë
function($){
$.fn.yourPluginName = function(){
// Your code goes here
return this;
};
})(jQuery);
Tip 10£º±¾µØ´æ´¢
Local storageÊÇÒ»¸öÓÃÓÚÔÚ¿Í»§¶ËÉÏ´æ´¢ÐÅÏ¢µÄAPI¡£Ê¹ÓÃʱ£¬ÄãÖ»Ð轫ÄãµÄÊý¾Ý×÷ΪlocalStorageÈ«¾Ö¶ÔÏóµÄÒ»¸öÊôÐÔ£º
Javascript´úÂë
localStorage.someData = "This data is going to persist across page refreshes and browser restarts";
¾ÉµÄä¯ÀÀÆ÷²»Ö§³Ö¸ÃAPI£¬²»¹ýÓи÷ÖÖjQuery²å¼þ¿ÉÒÔ×÷ΪÌæ´ú·½°¸¡£ÕâЩ²å¼þÔÚlocalStorage²»¿ÉÓÃʱÌṩÁËÆäËû´æ´¢·½°¸¡£ÏÂÃæÊÇÒ»¸öÀý×Ó£º
Javascript´úÂë
// Check if "key" exists in the storage.
var value = $.jStorage.get("key");
if(!value){
// if not - load the data from the server
value = load_data_from_server();
// and save it
$.jStorage.set("key",value);
Tip 11: Liveʼþ´¦Àí
ΪÈκÎÆ¥ÅäÑ¡ÔñÆ÷µÄÔªËØÉèÖÃÒ»¸öʼþ´¦Àí³ÌÐò£¬¼´Ê¹ËüÔÚ³õʼҳÃæ¼ÓÔغó±»Ìí¼Óµ½DOM£º
Javascript´úÂë
$('button.yourClassName').live('click', yourFunctionName);
ÕâÑù£¬Í¨¹ýajax»òjavascript¼ÓÔØÔªËØʱ£¬Ê¼þ´¦Àí³ÌÐò»á×Ô¶¯ÎªÕâЩԪËؽøÐÐÉèÖãº
Javascript´úÂë
$('button.yourClassName').die('click', yourFunctionName);
¾¡¹ÜÓë³£¹æʼþÏà±È£¬liveʼþ´¦Àí³ÌÐòÓÐһЩ¾ÖÏÞÐÔ£¬µ«Ëü»¹ÊÇÊÊÓÃÓڴ󲿷ÖÇé¿ö¡£LiveʼþÖ§³ÖjQuery 1.3¼°ÒÔÉÏ°æ±¾¡£
Tip 12£º¿Ë¡һ¸ö¶ÔÏó
ʹÓÃ.clone()·½·¨¿Ë¡JavaScriptÖеÄDOM¶ÔÏó£º
Javascript´úÂë
// Clone the DIV
var cloned = $('#yourdivID').clone();
.clone()·½·¨ÎÞ·¨¿Ë¡JavaScript¶ÔÏó¡£Èç¹ûÒª¿Ë¡JavaScript¶ÔÏó£¬Äã¿ÉÒÔʹÓÃÏÂÃæµÄ´úÂ룺
Javascript´úÂë
// Shallow copy
var newObject = jQuery.extend({}, oldObject);
// Deep copy
var newObject = jQuery.extend(true, {}, oldObject);
Tip 13£º²âÊÔÒþ²ØÔªËØ
ͨ¹ý.hide()ºÍ.show()·½·¨¿ÉÒԸıäÔªËصĿɼûÐÔ¡£Ê¹ÓÃÏÂÃæµÄ´úÂë¿ÉÒÔ¼ì²âÔªËØÊÇ·ñ¿É¼û£º
Javascript´úÂë
if($(element).is(":visible") == "true") {
//The element is Visible
}
Tip 14£ºÕÒ³ö×î½üµÄ¸¸DIV
Èç¹ûÄãÏëÕÒ³öij¸öÔªËصĸ¸¼¶DIV£¨ÎÞÂÛ¸ÃDIVÊÇ·ñÓÐID£©£¬ÄÇôÄã¿ÉÒÔʹÓÃÕâ¸öjQueryÑ¡ÔñÆ÷£º
Javascript´úÂë
$("#yourControl").closest("div");
¡¡