/* * my-js Document * author : sunjiawei at 2010-10-8 * mailto : sunjiawei1986@163.com */ /******************** prototype ********************/ var fn = Function.prototype; fn.extend = function(foo){ foo.call(this); } var date = Date.prototype; date.toString = function(format){ var year = this.getFullYear(),month = this.getMonth(),day = this.getDate(); if(format){ return format.replace(/yyyy/gi,year).replace(/yy/gi,year.toString().substr(2,2)).replace(/m/gi,month+1).replace(/d/gi,day); }else{return (year + "-" + (month + 1) + "-" + day);} } Date.diff = date.diff = function(lastDate,firstDate){ if(!firstDate){firstDate = this;} var diff = {}; diff.days = Math.floor((lastDate.getTime() - firstDate.getTime())/(1000*3600*24)); diff.years = parseFloat(diff.days/365).toFixed(2); diff.months = parseFloat(diff.days/30).toFixed(2); diff.hours = diff.days*24; diff.seconds = diff.days*24*3600; diff.mins = diff.days*24*60; return diff; } Date.get = function(dateString){ if(dateString){ var re = /(\d{2,4}).+(\d{1,2}).+(\d{1,2})/g; var isCurrect = re.test(dateString); if(isCurrect){return new Date(RegExp.$1,RegExp.$2,RegExp.$3);} } return new Date(); } var string = String.prototype; string.toNum = function(){return Number(this.replace(/\D/gi,""));} string.trim = function(isGlobal){ if(isGlobal){return this.replace(/\s+/g,"");} else{return this.replace(/(^\s+)|(\s+$)/g,"");} } var array = Array.prototype; array.insert = function(index,element){ this.splice(index,0,element); } array.remove = function(index){ this.splice(index,1); } array.replace = function(index,element){ this.splice(index,1,element); } array.include = function(element){ var count = this.length; var i = 0; var indexes = []; while(i < count){ if(this[i] == element){indexes.push(i);}i++; } return ((indexes.length < 1) ? -1 : indexes); } array.clone = function(cloner){ this.clear(); var count = cloner.length; for(var i=0;i= 0){ var result = foo(this[i]); if(isReverse){ if(result){ this.remove(i); } }else{ if(!result){ this.remove(i); } } i--; } } array.distance = function(){ var count = this.length; var i = count - 1; while(i >= 0){ var indexes = this.include(this[i]); if(indexes.length && indexes.length > 1){ this.remove(i); } i--; } } array.clear = function(){ this.length = 0; } /******************** prototype end ********************/ /******************** myjs ********************/ var $ = function(obj,scoper){ if(scoper){ if(scoper.length && scoper.instance){ var count = scoper.length; for(var i=0;i 0); e.pageX = e.clientX + document.body.scrollLeft; e.pageY = e.clientY + document.body.scrollTop; e.preventDefault = function(){this.returnValue = false;} if(e.type == "mouseover"){ e.relateElement = e.fromElement; }else if(e.type == "mouseout"){ e.relateElement = e.toElement; } e.stopPropagation = function(){this.cancleBubble = true;} e.target = e.srcElement; e.time = (new Date()).getTime(); } return e; } }, compatMode : function(){ return ((document.compatMode.toLowerCase() == "css1compat") ? true : false); }, /* 作用:得到指定对象的位置 参数: @target : 指定对象 返回值: 对象的left(x)和top(y)值 */ xy : function(target){ var targetTop = target.offsetTop; var targetLeft = target.offsetLeft; while(target = target.offsetParent){ targetTop += target.offsetTop; targetLeft += target.offsetLeft; } return { x : targetLeft, y : targetTop } }, // 样式 rules : { /* 作用:动态添加css样式 参数: @selector : css选择器名称(如body/.txtCss) @cssText : css样式 @index : 索引(添加到当前样式的第几个位置) */ add : function(selector,cssText,index){ var heads = $t("head"); var head = (heads.length) ? heads[0] : heads; var sheet = null; if(document.styleSheets.length < 1){ $c("style").attr({type : "text/css"}).appendTo(head); } sheet = document.styleSheets[document.styleSheets.length - 1]; if(sheet.addRule){ sheet.addRule(selector,cssText,index); }else if(sheet.insertRule){ sheet.insertRule(selector + "{" + cssText + "}",index); } }, /* 作用:动态删除css样式 参数: @index : css样式的索引 */ remove : function(index){ var sheet = null; if(document.styleSheets.length < 1){return false;} else{sheet = document.styleSheets[document.styleSheets.length - 1];} if(sheet.removeRule){sheet.removeRule(index);} else if(sheet.deleteRule){sheet.deleteRule(index);} }, /* 作用:获取css样式 参数: @selector : css选择器名称 @index : style的索引 文档中第几个style标签 默认为第一个 返回值: 若有selector 则返回单个样式对象 若无selector 则返回全部样式集合 */ get : function(selector,index){ var sheet = document.styleSheets[document.styleSheets.length - 1]; if(index){sheet = document.styleSheets[index];} var rules = (sheet.rules ? sheet.rules : sheet.cssRules); if(selector){ for(var i=0;i 0){ var c_start = cookie.indexOf(key + "="); if(c_start != -1){ c_start = c_start + key.length + 1; c_end = cookie.indexOf(";",c_start); if(c_end == -1){c_end = cookie.length;} return unescape(cookie.substring(c_start,c_end)); } } return null; } this.remove = function(key){ var key = key || this.key; var expires = Date.get(); expires.setTime(expires.getTime() - (expires.getTime() + 1)); document.cookie = key + "=my;expires=" + expires.toGMTString(); if((!key) || (this.key == key)){this.clear();} } this.clear = function(){ this.key = null; this.value = null; this.expires = null; this.domain = null; this.path = null; this.secure = null; this.cook = ""; } this.isExist = function(key){ var value = this.get(key); if(value != null && value != ""){return value;} return false; } this.set(key,value,expires,domain,path,secure); if(this.key != null && this.value != null){ this.create(); } }, // another expand // extend : function(namespace,json){ // // = fn; // var cls = my[namespace] = {}; // for(var key in json){ // cls[key] = json[key]; // } // }, ajax : { args : { xhr : null, url : null, analy : true, sendType : "GET", sendData : null, callback : null }, request : function(){ try{my.ajax.args.xhr = new ActiveXObject("Msxml2.XMLHTTP");} catch(e){ try{my.ajax.args.xhr = new ActiveXObject("Microsoft.XMLHTTP");} catch(e){ try{my.ajax.args.xhr = new XMLHttpRequest();} catch(e){my.ajax.args.xhr = null;} } } if(!my.ajax.args.xhr){return false;} else{ my.ajax.args.xhr.open(my.ajax.args.sendType,my.ajax.args.url,my.ajax.args.analy); // header my.ajax.args.xhr.onreadystatechange = my.ajax.args.callback; my.ajax.args.xhr.send(my.ajax.args.sendData); //return my.ajax.args.xhr; } }/*, receive : function(){ if(my.ajax.args.xhr){ if(my.ajax.args.xhr.readyState == 4){ if(my.ajax.args.xhr.status == 200){ return true; } } } },*/ } } window.my = window.M = my; /******************** myjs end ********************/