1、原生对象克隆
var clone = function(obj) {
var o;
if (typeof obj == "object") {
if (obj === null) {
o = null;
} else {
if (obj instanceof Array) {
o = [];
for (var i = 0,
len = obj.length; i < len; i++) {
o.push(clone(obj[i]));
}
} else {
o = {};
for (var j in obj) {
o[j] = clone(obj[j]);
}
}
}
} else {
o = obj;
}
return o;
};
2、数组循环
arr = [4,5,6].map(function(item, idx, origin){return item+1;});
arr = [4,5,6].forEach(function(item, idx, origin){console.log(item+1);});
arr = [4,5,6].filter(function(item, idx, origin){return item>4;});
3、数组相减
var arrSub = function(mainArr, subArr, eqFn) {
var arr1 = mainArr.concat([]);
var a, b;
for (var i = arr1.length - 1; i >= 0; i--) {
a = arr1[i];
for (var j = subArr.length - 1; j >= 0; j--) {
b = subArr[j];
if (eqFn === undefined) {
if (a == b) {
arr1.splice(i, 1);
break;
}
} else if (eqFn(a, b)) {
arr1.splice(i, 1);
break;
}
}
}
return arr1;
};
arrSub([ 1, 2 ], [ 1 ]);
4、数组去重
var uniq = function(arr){
var newArr = [];
for(var i = 0; i < arr.length; i++){
if(newArr.indexOf(arr[i]) == -1)newArr.push(arr[i]);
}
return newArr;
};
// or:
Array.prototype.uniq = function(){
var newArr = [];
for(var i = 0; i < this.length; i++){
if(newArr.indexOf(this[i]) == -1)newArr.push(this[i]);
}
return newArr;
};
5、bind兼容
if (!Function.prototype.bind) {
Function.prototype.bind = function(obj) {
var _self = this, args = arguments;
return function() {
_self.apply(obj, Array.prototype.slice.call(args, 1));
}
};
}
6、日期转字符串
Date.prototype.format = function(format) {
var date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S+": this.getMilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}; console.log((new Date()).format("yyyy-MM-dd"));
7、日期加法
Date.prototype.DateAdd = function (strInterval, Number) {
var dtTmp = this;
switch (strInterval) {
case 's': return new Date(Date.parse(dtTmp) + (1000 * Number));
case 'n': return new Date(Date.parse(dtTmp) + (60000 * Number));
case 'h': return new Date(Date.parse(dtTmp) + (3600000 * Number));
case 'd': return new Date(Date.parse(dtTmp) + (86400000 * Number));
case 'w': return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
case 'q': return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number * 3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
case 'm': return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
case 'y': return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
}
}
8、浏览器类型
function getOs() {
if (navigator.userAgent.indexOf("MSIE") > 0)return 1;//IE
if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0)return 2;//Firefox
if (isSafari = navigator.userAgent.indexOf("Chrome") > 0)return 3;//Chrome
if (isSafari = navigator.userAgent.indexOf("Safari") > 0)return 4;//Safari
if (isCamino = navigator.userAgent.indexOf("Camino") > 0)return 5;//Camino
if (isMozilla = navigator.userAgent.indexOf("Gecko/") > 0)return 6;//Gecko
//other...
return 0;
} //Jquery:
$.browser.msie == true
$.browser.safari == true
$.browser.opera == true
$.browser.mozilla == true
9、event.srcElement和event.target
ie支持前者,firefox和chrome等浏览器支持后者。但ie下可直接使用event(window.event),firefox不能直接使用event。解决办法:
function myfunc()
{
var evt = getEvent();
var element = evt.srcElement || evt.target;
} function getEvent()
{
if (document.all)
{
return window.event;//如果是ie
}
func = getEvent.caller;
while (func != null)
{
var arg0 = func.arguments[0];
if (arg0)
{
if ((arg0.constructor == Event || arg0.constructor == MouseEvent) || (typeof(arg0) == "object" && arg0.preventDefault && arg0.stopPropagation))
{
return arg0;
}
}
func = func.caller;
}
return null;
}
10、用鼠标滚轮控制图片大小
//img onmousewheel="return bbimg(this)"
function bbimg(o) {
var zoom = parseInt(o.style.zoom, 10) || 100;
zoom += event.wheelDelta / 12;
if (zoom > 0) o.style.zoom = zoom + '%';
return false;
}