AJax¼¼Êõ

ÈËÈ˶¼»áµÄ35¸öJqueryС¼¼ÇÉ(2)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2015-10-28 16:26 ÎÒÒªÆÀÂÛ( )

Äã¿ÉÒÔÀûÓà animate ºÍ scrollTop À´ÊµÏÖ·µ»Ø¶¥²¿µÄ¶¯»­£¬¶ø²»ÐèҪʹÓÃÆäËû²å¼þ¡£ // Back to top$('a.top').click(function () { $(document.body).animate({scrollTop: 0}, 800); return false;});!-- Create an

Äã¿ÉÒÔÀûÓà animate ºÍ scrollTop À´ÊµÏÖ·µ»Ø¶¥²¿µÄ¶¯»­£¬¶ø²»ÐèҪʹÓÃÆäËû²å¼þ¡£

// Back to top $('a.top').click(function () { $(document.body).animate({scrollTop: 0}, 800); return false; }); <!-- Create an anchor tag --> <a href="#">Back to top</a>

¸Ä±ä scrollTop µÄÖµ¿ÉÒÔµ÷Õû·µ»Ø¾àÀ붥²¿µÄ¾àÀ룬¶ø animate µÄµÚ¶þ¸ö²ÎÊýÊÇÖ´Ðзµ»Ø¶¯×÷ÐèÒªµÄʱ¼ä(µ¥Î»£ººÁÃë)¡£

12.Ô¤¼ÓÔØͼƬ

Èç¹ûÄãµÄÒ³ÃæÖÐʹÓÃÁ˺ܶ಻¿É¼ûµÄͼƬ£¨È磺hover ÏÔʾ£©£¬Äã¿ÉÄÜÐèÒªÔ¤¼ÓÔØËüÃÇ£º

$.preloadImages = function () { for (var i = 0; i < arguments.length; i++) { $('<img>').attr('src', arguments[i]); } }; $.preloadImages('img/hover1.png', 'img/hover2.png'); 13.¼ì²éͼƬÊÇ·ñ¼ÓÔØÍê³É

ÓÐʱºòÄãÐèҪȷ±£Í¼Æ¬Íê³É¼ÓÔØÍê³ÉÒÔ±ãÖ´ÐкóÃæµÄ²Ù×÷£º

$('img').load(function () { console.log('image load successful'); });

Äã¿ÉÒÔ°Ñ img Ì滻ΪÆäËûµÄ ID »òÕß class À´¼ì²éÖ¸¶¨Í¼Æ¬ÊÇ·ñ¼ÓÔØÍê³É¡£

14.×Ô¶¯ÐÞ¸ÄÆÆËðͼÏñ

Èç¹ûÄãÅöÇÉÔÚÄãµÄÍøÕ¾ÉÏ·¢ÏÖÁËÆÆËéµÄͼÏñÁ´½Ó£¬Äã¿ÉÒÔÓÃÒ»¸ö²»Ò×±»Ìæ»»µÄͼÏñÀ´´úÌæËüÃÇ¡£Ìí¼ÓÕâ¸ö¼òµ¥µÄ´úÂë¿ÉÒÔ½ÚÊ¡ºÜ¶àÂé·³£º

$('img').on('error', function () { $(this).prop('src', 'img/broken.png'); });

¼´Ê¹ÄãµÄÍøվûÓÐÆÆËéµÄͼÏñÁ´½Ó£¬Ìí¼ÓÕâ¶Î´úÂëҲûÓÐÈκκ¦´¦¡£

15.Êó±êÐüÍ£(hover)Çл» class ÊôÐÔ

¼ÙÈçµ±Óû§Êó±êÐüÍ£ÔÚÒ»¸ö¿Éµã»÷µÄÔªËØÉÏʱ£¬ÄãÏ£Íû¸Ä±äÆäЧ¹û£¬ÏÂÃæÕâ¶Î´úÂë¿ÉÒÔÔÚÆäÐüÍ£ÔÚÔªËØÉÏʱÌí¼Ó class ÊôÐÔ£¬µ±Óû§Êó±êÀ뿪ʱ£¬Ôò×Ô¶¯È¡Ïû¸Ã class ÊôÐÔ£º

$('.btn').hover(function () { $(this).addClass('hover'); }, function () { $(this).removeClass('hover'); });

ÄãÖ»ÐèÒªÌí¼Ó±ØÒªµÄCSS´úÂë¼´¿É¡£Èç¹ûÄãÏëÒª¸ü¼ò½àµÄ´úÂ룬¿ÉÒÔʹÓà toggleClass ·½·¨£º

$('.btn').hover(function () { $(this).toggleClass('hover'); });

×¢£ºÖ±½ÓʹÓÃCSSʵÏÖ¸ÃЧ¹û¿ÉÄÜÊǸüºÃµÄ½â¾ö·½°¸£¬µ«ÄãÈÔÈ»ÓбØÒªÖªµÀ¸Ã·½·¨¡£

16.½ûÓà input ×Ö¶Î

ÓÐʱÄã¿ÉÄÜÐèÒª½ûÓÃ±íµ¥µÄ submit °´Å¥»òÕßij¸ö input ×ֶΣ¬Ö±µ½Óû§Ö´ÐÐÁËijЩ²Ù×÷£¨ÀýÈ磬¼ì²é¡°ÒÑÔĶÁÌõ¿î¡±¸´Ñ¡¿ò£©¡£¿ÉÒÔÌí¼Ó disabled ÊôÐÔ£¬Ö±µ½ÄãÏëÆôÓÃËüʱ£º

$('input[type="submit"]').prop('disabled', true);

ÄãÒª×öµÄ¾ÍÊÇÖ´ÐÐ removeAttr ·½·¨£¬²¢°ÑÒªÒƳýµÄÊôÐÔ×÷Ϊ²ÎÊý´«È룺

$('input[type="submit"]').removeAttr('disabled'); 17.×èÖ¹Á´½Ó¼ÓÔØ

ÓÐʱÄ㲻ϣÍûÁ´½Óµ½Ä³¸öÒ³Ãæ»òÕßÖØмÓÔØËü£¬Äã¿ÉÄÜÏ£ÍûËüÀ´×öһЩÆäËûÊÂÇé»òÕß´¥·¢Ò»Ð©ÆäËû½Å±¾£¬Äã¿ÉÒÔÕâô×ö£º

$('a.no-link').click(function (e) { e.preventDefault(); }); 18.Çл» fade/slide

fade ºÍ slide ÊÇÎÒÃÇÔÚ jQuery Öо­³£Ê¹ÓõĶ¯»­Ð§¹û£¬ËüÃÇ¿ÉÒÔʹԪËØÏÔʾЧ¹û¸üºÃ¡£µ«ÊÇÈç¹ûÄãÏ£ÍûÔªËØÏÔʾʱʹÓõÚÒ»ÖÖЧ¹û£¬¶øÏûʧʱʹÓõڶþÖÖЧ¹û£¬Ôò¿ÉÒÔÕâô×ö£º

// Fade $('.btn').click(function () { $('.element').fadeToggle('slow'); }); // Toggle $('.btn').click(function () { $('.element').slideToggle('slow'); }); 19.¼òµ¥µÄÊÖ·çÇÙЧ¹û

ÕâÊÇÒ»¸öʵÏÖÊÖ·çÇÙЧ¹û¿ìËÙ¼òµ¥µÄ·½·¨£º

// Close all panels $('#accordion').find('.content').hide(); // Accordion $('#accordion').find('.accordion-header').click(function () { var next = $(this).next(); next.slideToggle('fast'); $('.content').not(next).slideUp('fast'); return false; }); 20.ÈÃÁ½¸ö DIV ¸ß¶ÈÏàͬ

ÓÐʱÄãÐèÒªÈÃÁ½¸ö div ¸ß¶ÈÏàͬ£¬¶ø²»¹ÜËüÃÇÀïÃæµÄÄÚÈݶàÉÙ¡£¿ÉÒÔʹÓÃÏÂÃæµÄ´úÂëƬ¶Î£º

var $columns = $('.column'); var height = 0; $columns.each(function () { if ($(this).height() > height) { height = $(this).height(); } }); $columns.height(height);

Õâ¶Î´úÂë»áÑ­»·Ò»×éÔªËØ£¬²¢ÉèÖÃËüÃǵĸ߶ÈΪԪËØÖеÄ×î´ó¸ß¡£

21. ÑéÖ¤ÔªËØÊÇ·ñΪ¿Õ

This will allow you to check if an element is empty.

$(document).ready(function() { if ($('#id').html()) { // do something } }); 22. Ìæ»»ÔªËØ

Want to replace a div, or something else?

$(document).ready(function() { $('#id').replaceWith(' <DIV>I have been replaced</DIV> '); }); 23. jQueryÑÓʱ¼ÓÔع¦ÄÜ

Want to delay something?

$(document).ready(function() { window.setTimeout(function() { // do something }, 1000); }); 24. ÒƳýµ¥´Ê¹¦ÄÜ

Want to remove a certain word(s)?

$(document).ready(function() { var el = $('#id'); el.html(el.html().replace(/word/ig, "")); }); 25. ÑéÖ¤ÔªËØÊÇ·ñ´æÔÚÓÚjquery¶ÔÏ󼯺ÏÖÐ

Simply test with the .length property if the element exists.

$(document).ready(function() { if ($('#id').length) { // do something } }); 26. ʹÕû¸öDIV¿Éµã»÷

Want to make the complete div clickable?

$(document).ready(function() { $("div").click(function(){ //get the url from href attribute and launch the url window.location=$(this).find("a").attr("href"); return false; }); // how to use <DIV><A href="index.html">home</A></DIV> }); 27. IDÓëClassÖ®¼äת»»

µ±¸Ä±äWindow´óСʱ£¬ÔÚIDÓëClassÖ®¼äÇл»

$(document).ready(function() { function checkwindowsize() { if ( $(window).width() > 1200 ) { $('body').addClass('large'); } else { $('body').removeClass('large'); } } $(window).resize(checkWindowSize); }); 28. ¿Ë¡¶ÔÏó

Clone a div or an other element.

$(document).ready(function() { var cloned = $('#id').clone(); // how to use <DIV id=id></DIV> }); 29. ʹԪËؾÓÆÁÄ»ÖмäλÖÃ

Center an element in the center of your screen.

$(document).ready(function() { jQuery.fn.center = function () { this.css("position","absolute"); this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px"); this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px"); return this; } $("#id").center(); }); 30. д×Ô¼ºµÄÑ¡ÔñÆ÷

¡¡

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

Ïà¹ØÎÄÕÂ
  • Can You Now Trust Google To Crawl Ajax Sites?

    Can You Now Trust Google To Crawl Ajax Sites?

    2015-11-19 15:49

  • ajaxʵÏÖÎļþÏÂÔØ

    ajaxʵÏÖÎļþÏÂÔØ

    2015-11-13 11:20

  • JavaScriptȨÍþÉè¼Æ--jQuery,Ajax.animate,SVG(¼òҪѧϰ±Ê¼Ç¶þÊ®)[Íê½áƪ]

    JavaScriptȨÍþÉè¼Æ--jQuery,Ajax.animate,SVG(¼òҪѧϰ±Ê¼Ç¶þÊ®)[Íê

    2015-10-28 08:39

ÍøÓѵãÆÀ
½