Äã¿ÉÒÔÀûÓà 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/slidefade ºÍ 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. д×Ô¼ºµÄÑ¡ÔñÆ÷
¡¡