ʾÀý
// ²é¿´¶ÔÓ¦ÔªËصÄλÖà var arr = [ 4, "Pete", 8, "John" ]; jQuery.inArray("John", arr); //3 jQuery.inArray(4, arr); //0 jQuery.inArray("David", arr); //-1 jQuery.inArray("Pete", arr, 2); //-1 2.6 ºÏ²¢Êý×é·½·¨mergeÓï·¨£ºjQuery.merge(first,second)
¸ÅÊö
ºÏ²¢Á½¸öÊý×é·µ»ØµÄ½á¹û»áÐ޸ĵÚÒ»¸öÊý×éµÄÄÚÈÝ¡ª¡ªµÚÒ»¸öÊý×éµÄÔªËغóÃæ¸ú×ŵڶþ¸öÊý×éµÄÔªËØ¡£ÒªÈ¥³ýÖظ´ÏÇëʹÓÃ$.unique()
²ÎÊý
ʾÀý
// ºÏ²¢Á½¸öÊý×éµ½µÚÒ»¸öÊý×éÉÏ¡£ $.merge( [0,1,2], [2,3,4] ) // ½á¹û: // [0,1,2,2,3,4]Óï·¨£ºjQuery.unique(array)
¸ÅÊö
ɾ³ýÊý×éÖÐÖظ´ÔªËØ¡£Ö»´¦Àíɾ³ýDOMÔªËØÊý×飬¶ø²»ÄÜ´¦Àí×Ö·û´®»òÕßÊý×ÖÊý×é¡£
ʾÀý
// ɾ³ýÖظ´ div ±êÇ©¡£ $.unique(document.getElementsByTagName("div")); // ½á¹û: // [<div>, <div>, ...]Óï·¨£ºjQuery.extend([deep], target, object1, [objectN])
¸ÅÊö
ÓÃÒ»¸ö»ò¶à¸öÆäËû¶ÔÏóÀ´À©Õ¹Ò»¸ö¶ÔÏ󣬷µ»Ø±»À©Õ¹µÄ¶ÔÏó¡£Èç¹û²»Ö¸¶¨target£¬Ôò¸øjQueryÃüÃû¿Õ¼ä±¾Éí½øÐÐÀ©Õ¹¡£ÕâÓÐÖúÓÚ²å¼þ×÷ÕßΪjQueryÔö¼Óз½·¨¡£ Èç¹ûµÚÒ»¸ö²ÎÊýÉèÖÃΪtrue£¬ÔòjQuery·µ»ØÒ»¸öÉî²ã´ÎµÄ¸±±¾£¬µÝ¹éµØ¸´ÖÆÕÒµ½µÄÈκζÔÏó¡£·ñÔòµÄ»°£¬¸±±¾»áÓëÔ¶ÔÏó¹²Ïí½á¹¹¡£ 䶨ÒåµÄÊôÐÔ½«²»»á±»¸´ÖÆ£¬È»¶ø´Ó¶ÔÏóµÄÔÐͼ̳еÄÊôÐÔ½«»á±»¸´ÖÆ¡£
²ÎÊý
ʾÀý
// ºÏ²¢ settings ºÍ options£¬Ð޸IJ¢·µ»Ø settings¡£ var settings = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; jQuery.extend(settings, options); // ½á¹û: // settings == { validate: true, limit: 5, name: "bar" }ÉϿδúÂë
// É±´ var target = {name: 'laoma'}; var obj1 = {age: 18, cellphone: '18911865673', mail: 'malun666@126.com'}; var p1 = {k: 123, m: 'abc'}; var obj2 = {run: function() { console.log(1); }, p: p1}; var newObj = jQuery.extend(true, target, obj1, obj2); console.log(newObj, target); console.log(newObj === target) // console.log(newObj.p === p1); console.log(newObj.p.k); //123 p1.k = 444; //newObj.p.k console.log(newObj.p.k); //123 console.log('p1.k', p1.k); //444 2.9 ÆäËû¹¹Ô캯ÊýÉϵķ½·¨ºÍÊôÐÔÊôÐÔÃû ʵÀý ˵Ã÷
noop var f = jQuery.noop; Ò»¸ö¿Õº¯Êý
isArray $.isArray([1,3,4]) ²âÊÔ¶ÔÏóÊÇ·ñΪÊý×é¡£
isFunction jQuery.isFunction(obj) ²âÊÔ¶ÔÏóÊÇ·ñΪº¯Êý¡£
isNumeric jQuery.isNumeric(value) È·¶¨ËüµÄ²ÎÊýÊÇ·ñÊÇÒ»¸öÊý×Ö¡£
isWindow jQuery.isWindow(obj) ²âÊÔ¶ÔÏóÊÇ·ñÊÇ´°¿Ú
error jQuery.error(message) ½ÓÊÜÒ»¸ö×Ö·û´®£¬²¢ÇÒÖ±½ÓÅ׳öÒ»¸ö°üº¬Õâ¸ö×Ö·û´®µÄÒì³£
trim jQuery.trim(str) È¥µô×Ö·û´®ÆðʼºÍ½áβµÄ¿Õ¸ñ
3.Á´Ê½±à³ÌºÍÒþʽµü´ú 3.1 Á´Ê½±à³ÌÓÉÓڴ󲿷ÖjQueryµÄapi·½·¨ÄÚ²¿·µ»ØÖµ¶¼ÊÇjQueryµÄ°ü×°¶ÔÏó×ÔÉí¡£ËùÒÔÎÒÃÇ¿ÉÒÔÔÚjQueryµÄapiµ÷ÓÃÖ®ºó¼ÌÐøµ÷ÓÃjQueryµÄ·½·¨£¬ÕâÑù¾Í³Æ×÷ÊÇÁ´Ê½±à³Ì¡£
ÀýÈç´úÂë:
$('#p1').css('color', 'red').height(200).hide('slow'); // µÈ¼ÛÓÚ $('#p1').css('color', 'red'); $('#p1').height(200); $('#p1').hide('slow');ÓÉÓÚcss·½·¨¡¢height¡¢hide·½·¨¶¼·µ»ØjQuery°ü×°¶ÔÏó×ÔÉí¡£ËùÒԾͿÉÒÔ¼ÌÐøÁ´Ê½µ÷Óá£
Á´Ê½±à³ÌµÄÔÀí
//Á´Ê½±à³ÌµÄÔÀí£º¶ÔÏóµ÷ÓÃÁË·½·¨ºó£¬·½·¨·µ»Øµ±Ç°¶ÔÏó¡£ var cat = { run: function() { console.log('runing'); return this; // ºËÐÄ£º·½·¨ÄÚ²¿Óְѵ±Ç°¶ÔÏó·µ»ØÁË¡£ }, sayHi: function() { console.log('hi'); return this; }, jump: function() { console.log('jump'); return this; } }; cat.run().sayHi().jump();ÓÐЩ·½·¨¿ÉÒÔÆÆ»µÁ´Ê½µÄ½á¹¹£¬±ÈÈ磺
nextAll(),prevAll(),sibilings(),find(),children(),parent(),parents()...
Èç¹ûÏë»Øµ½×î½üÒ»´ÎÆÆ»µÁ´Ê½½á¹¹Ö®Ç°µÄ´úÂë¿ÉÒÔʹÓÃend·½·¨¡£
$('#p1').nextAll().hide().end().css('color', 'red');
3.2 Òþʽµü´újQuery°ü×°¶ÔÏó±¾Éí¾ÍÊÇÒ»¸öαÊý×飬ƥÅäµÄÔªËØÓжà¸öµÄʱºò£¬Òª×öÉèÖòÙ×÷µÄʱºò£¬jQueryÄÚ²¿»áÒþʽµÄ±äÁ¿ËùÓеÄÆ¥ÅäÔªËص÷ÓÃÉèÖòÙ×÷£¬ËùÒÔ³ÆΪÒþʽµü´ú¡£
ÆÀ·Ö¿Ø¼þ°¸Àý
<ul> <li>¡î</li> <li>¡î</li> <li>¡î</li> <li>¡î</li> <li>¡î</li> </ul> <script src="./lib/jquery-1.12.4.js"></script> <script> $(function() { // µÚÒ»¸öЧ¹û£º µ±Êó±êÒÆÈë Îå½ÇÐÇʱºò¡£Ö®Ç°µÄÎå½ÇÐǺÍ×Ô¼º¶¼±ä³ÉʵÐĵÄÎå½ÇÐÇ $('.list li').hover(function(e) { // Êó±êÒÆÈ룺µ±Êó±êÒÆÈë Îå½ÇÐÇʱºò¡£Ö®Ç°µÄÎå½ÇÐǺÍ×Ô¼º¶¼±ä³ÉʵÐĵÄÎå½ÇÐÇ $(this).text('¡ï').prevAll().text('¡ï'); // Òþʽµü´ú¡£ }, function(e) { // Êó±êÒƳö£º °Ñ×Ô¼º±ä³É¿ÕÐÄÎå½ÇÐÇ£¬¶øÇÒºóÃæµÄÒ²¶¼±ä³É¿ÕÐÄÎå½ÇÐÇ $(this).text('¡î').nextAll().text('¡î'); }).on('click', function(e) { // µã»÷Ö®ºó£¬¼Ç¼µ±Ç°µã»÷µÄÊÇË $(this).addClass('cur').siblings().removeClass('cur'); }); // ¸øÕû¸öul±êÇ©°ó¶¨Ò»¸ömouseleaveʼþ¡£ $('.list').on('mouseleave', function(e) { // Äõ½µ±Ç° cur ÀàµÄli±êÇ©¡£ $('.list li.cur').text('¡ï') .prevAll().text('¡ï'). // Ç°ÃæµÄ½Úµã¶¼ÉèÖóÉʵÐÄ end().nextAll().text('¡î');// µã»÷ÔªËغóÃæµÄ½ÚµãÉèÖóɿÕÐÄ }); }); </script> 4.jQueryµÄ²å¼þ·â×° 4.1 ¸øjQuery°ü×°¶ÔÏóÀ©Õ¹·½·¨ÊôÐÔ
¡¡