jQuery¼¼Êõ

ÀÏÂíjQuery½Ì³Ì(2)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-12-19 09:15 ÎÒÒªÆÀÂÛ( )

ʾÀý // ²é¿´¶ÔÓ¦ÔªËصÄλÖÃvar arr = [ 4, "Pete", 8, "John" ];jQuery.inArray("John", arr); //3jQuery.inArray(4, arr); //0jQuery.inArray("David", arr); //-1jQuery.inArray("Pete", arr, 2); //-1 2.6 ºÏ²¢

ʾÀý

// ²é¿´¶ÔÓ¦ÔªËصÄλÖà 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()

  • ²ÎÊý

  • first:µÚÒ»¸ö´ý´¦ÀíÊý×飬»á¸Ä±äÆäÖеÄÔªËØ¡£
  • second:µÚ¶þ¸ö´ý´¦ÀíÊý×飬²»»á¸Ä±äÆäÖеÄÔªËØ¡£
  • ʾÀý

    // ºÏ²¢Á½¸öÊý×éµ½µÚÒ»¸öÊý×éÉÏ¡£ $.merge( [0,1,2], [2,3,4] ) // ½á¹û: // [0,1,2,2,3,4]
  • 2.7 Êý×éÈ¥ÖØunique
  • Óï·¨£ºjQuery.unique(array)

  • ¸ÅÊö

    ɾ³ýÊý×éÖÐÖظ´ÔªËØ¡£Ö»´¦Àíɾ³ýDOMÔªËØÊý×飬¶ø²»ÄÜ´¦Àí×Ö·û´®»òÕßÊý×ÖÊý×é¡£

  • ʾÀý

    // ɾ³ýÖظ´ div ±êÇ©¡£ $.unique(document.getElementsByTagName("div")); // ½á¹û: // [<div>, <div>, ...]
  • 2.8 jQuery¹¹Ô캯ÊýµÄÀ©Õ¹¶ÔÏó·½·¨£¨¼Ì³Ð£©
  • Óï·¨£º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°ü×°¶ÔÏóÀ©Õ¹·½·¨ÊôÐÔ
  • Ö±½Ó¸ø$.fnÌí¼Ó·½·¨ºÍÊôÐÔ
  • (function(jQuery) { jQuery.fn.logText = function() { console.log(this.text()); }; })(jQuery); 4.2 ¸ø¹¹Ô캯ÊýÀ©Õ¹·½·¨ºÍÊôÐÔ
  • ͨ¹ý$.extend()À´À©Õ¹jQuery¹¹Ô캯Êý
  • $.extend({ log: function(message) { var now = new Date(), y = now.getFullYear(), m = now.getMonth() + 1, //£¡JavaScriptÖÐÔ·ÖÊÇ´Ó0¿ªÊ¼µÄ d = now.getDate(), h = now.getHours(), min = now.getMinutes(), s = now.getSeconds(), time = y + 'http://www.jianshu.com/' + m + 'http://www.jianshu.com/' + d + ' ' + h + ':' + min + ':' + s; console.log(time + ' My App: ' + message); } }); $.log('initializing...'); //µ÷ÓÃ
  • Ö±½Ó¸øjQuery¹¹Ô캯ÊýÌí¼ÓÊôÐԺͷ½·¨
  • (function(jQuery) { jQuery.appName = 'laoma Extend'; })(jQuery);

    ¡¡

    1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

    Ïà¹ØÎÄÕÂ
    • jQuery([selector,[context]])

      jQuery([selector,[context]])

      2017-12-19 16:32

    • [¸ßΣ]ʹÓôæÔÚ©¶´µÄJQuery°æ±¾½â¾ö·½·¨

      [¸ßΣ]ʹÓôæÔÚ©¶´µÄJQuery°æ±¾½â¾ö·½·¨

      2017-12-16 17:28

    • jquery.validate.js valid·½·¨¼°×¢ÒâÊÂÏî

      jquery.validate.js valid·½·¨¼°×¢ÒâÊÂÏî

      2017-12-16 12:19

    • ͨ¹ýÀý×ÓѧϰJqueryµ¼º½À¸ ²Ëµ¥

      ͨ¹ýÀý×ÓѧϰJqueryµ¼º½À¸ ²Ëµ¥

      2017-12-15 14:21

    ÍøÓѵãÆÀ
    Ì