非常好的javascript 代码
Posted 每天进步一点点!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了非常好的javascript 代码相关的知识,希望对你有一定的参考价值。
与其他javascript类库冲突解决方案 $(document).ready(function() { var $jq = jQuery.noConflict(); $jq(‘#id‘).show(); });
使元素居屏幕中间位置 $(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(); });
使整个DIV可点击 $(document).ready(function() { $("div").click(function() { window.location=$(this).find("a").attr("href"); return false; }); // how to use <DIV><A href="index.html">home</A></DIV> });
jQuery延时加载功能view plaincopy to clipboardprint? $(document).ready(function() { window.setTimeout(function() { }, 1000); });
获得鼠标指针XY值 $(document).ready(function() { $().mousemove(function(e) { $(‘#XY‘).html("X Axis : " + e.pageX + " | Y Axis " + e.pageY); }); // how to use <DIV id=XY></DIV> });
返回页面顶部功能 $(document).ready(function() { $(‘a[href*=#]‘).click(function() { if (location.pathname.replace(/^\//,‘‘) == this.pathname.replace(/^\//,‘‘) && location.hostname == this.hostname) { var $target = $(this.hash); $target = $target.length && $target || $(‘[name=‘ + this.hash.slice(1) +‘]‘); if ($target.length) { var targetOffset = $target.offset().top; $(‘html,body‘).animate({scrollTop: targetOffset}, 900); return false; } } }); // how to use <A name=top></A> <A href="#top">go to top</A> });
页面样式切换 $(document).ready(function() { $("a.Styleswitcher").click(function() { $(‘link[rel=stylesheet]‘).attr(‘href‘ , $(this).attr(‘rel‘)); }); // how to use <LINK href="default.css" type=text/css rel=stylesheet> // the links <A class=Styleswitcher href="#" rel=default.css>Default Theme</A> <A class=Styleswitcher href="#" rel=red.css>Red Theme</A> <A class=Styleswitcher href="#" rel=blue.css>Blue Theme</A> });
预加载图片 $(document).ready(function() { jQuery.preloadImages = function() { for(var i = 0; i").attr("src", arguments[i]); } }; // how to use $.preloadImages("image1.jpg"); });
禁止右键点击 $(document).ready(function() { $(document).bind("contextmenu",function(e) { return false; }); });
用下面方法,可以简单又可靠的得到一个变量是否一个数组:
Object.prototype.toString.apply(value) === ‘[object Array]‘