10个很棒的jQuery代码片段分别是什么

发布时间:2021-11-15 22:18:06 作者:柒染
来源:亿速云 阅读:88

本篇文章为大家展示了10个很棒的jQuery代码片段分别是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

图片预加载

(function($) {    var cache = [];    // Arguments are image paths relative to the current page.    $.preLoadImages = function() {      var args_len = arguments.length;      for (var i = args_len; i--;) {        var cacheImage = document.createElement('img');        cacheImage.src = arguments[i];        cache.push(cacheImage);      }    }   jQuery.preLoadImages("image1.gif", "/path/to/image2.png");

在新窗口打开链接 (target=”blank”)

$('a[@rel$='external']').click(function(){       this.target = "_blank";  });   /*     Usage:     <a href="http://www.catswhocode.com" rel="external">catswhocode.com</a> */

当支持 JavaScript 时为 body 增加 class

/* 该代码只有1行,但是最简单的用来检测浏览器是否支持 JavaScript 的方法,如果支持 JavaScript 就在 body 元素增加一个 hasJS 的 class */ $('body').addClass('hasJS');

平滑滚动页面到某个锚点

$(document).ready(function() {      $("a.topLink").click(function() {          $("html, body").animate({              scrollTop: $($(this).attr("href")).offset().top + "px"         }, {              duration: 500,              easing: "swing"         });          return false;      });  });

鼠标滑动时的渐入和渐出

$(document).ready(function(){      $(".thumbs img").fadeTo("slow", 0.6); // This sets the opacity of the thumbs to fade down to 60% when the page loads       $(".thumbs img").hover(function(){          $(this).fadeTo("slow", 1.0); // This should set the opacity to 100% on hover      },function(){          $(this).fadeTo("slow", 0.6); // This should set the opacity back to 60% on mouseout      });  });

制作等高的列

var max_height = 0;  $("div.col").each(function(){      if ($(this).height() > max_height) { max_height = $(this).height(); }  });  $("div.col").height(max_height);

在一些老的浏览器上启用 HTML5 的支持

(function(){      if(!/*@cc_on!@*/0)          return;      var e = "abbr,article,aside,audio,bb,canvas,datagrid,datalist,details,dialog,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video".split(','),i=e.length;while(i--){document.createElement(e[i])}  })()   //然后在head中引入该js  <!--[if lt IE 9]>  <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>  <![endif]-->

测试浏览器是否支持某些 CSS3 属性

var supports = (function() {     var div = document.createElement('div'),        vendors = 'Khtml Ms O Moz Webkit'.split(' '),        len = vendors.length;      return function(prop) {        if ( prop in div.style ) return true;         prop = prop.replace(/^[a-z]/, function(val) {           return val.toUpperCase();        });         while(len--) {           if ( vendors[len] + prop in div.style ) {              // browser supports box-shadow. Do what you need.              // Or use a bang (!) to test if the browser doesn't.              return true;           }        }        return false;     };  })();   if ( supports('textShadow') ) {     document.documentElement.className += ' textShadow';

获取 URL 中传递的参数

$.urlParam = function(name){      var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);      if (!results) { return 0; }      return results[1] || 0;  }

禁用表单的回车键提交

$("#form").keypress(function(e) {    if (e.which == 13) {      return false;    }  });

上述内容就是10个很棒的jQuery代码片段分别是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 常用的几个JQuery代码片段
  2. jQuery和Zepto分别是什么意思

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

jquery

上一篇:Web开发中Kendo UI for jQuery数据管理列模板是怎样的

下一篇:10个基于CSS3/jQuery的加载动画设计方案分别是怎样的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》