common
function randomColor(){
var R = randomInt(0,255);
var G = randomInt(0,255);
var B = randomInt(0,255);
return "rgb("+R+","+G+","+B+")";
}
function randomInt(min, max){
return Math.floor(Math.random()*(max-min)) + min;
}
function textNodefilter(nodelist){
var temp = [];
for(var i=0; i<nodelist.length; i++){
if(nodelist[i].nodeType == 1){
temp.push(nodelist[i]);
}
}
return temp;
}
function getStyle(ele){
if(ele.currentStyle) {
return ele.currentStyle;
} else {
return getComputedStyle(ele);
}
}
function toArray(arr){
var a = [];
for(var i = 0; i<arr.length; i++){
a.push(arr[i]);
}
return a;
}
function log(){
if(console && console.log){
console.log(arguments);
} else {
alert(arguments);
}
}
//计算一个dom元素的PageX/Y
function getPagePosition(target){
var sumLeft = target.offsetLeft;
var sumTop = target.offsetTop;
while(target.offsetParent != null){
sumLeft += target.offsetParent.offsetLeft;
sumTop += target.offsetParent.offsetTop;
target = target.offsetParent;
}
return {
pageX : sumLeft,
pageY : sumTop
};
}
//封装事件监听的添加
function addEventHandler(ele, eventType, fn, isCapture){
if(ele.addEventListener) {
ele.addEventListener(eventType, fn, isCapture);
} else {
ele.attachEvent("on"+eventType, fn);
}
}
//限定一个数字的大小范围
function section(val, min, max) {
return Math.max(min, Math.min(max, val));
}
function hide(ele) {
ele.style.display = "none";
}
function show(ele) {
ele.style.display = "block";
}
(function(){
if(!document.getElementsByClassName){
document.getElementsByClassName = function(classname){
var allEle = document.getElementsByTagName("*");
var temp = [];
for(var i=0; i<allEle.length; i++){
if( allEle[i].className.indexOf(classname) != -1){
temp.push( allEle[i] );
}
}
return temp;
}
}
})();
dateUtile(现在经常使用moment.sjs)
//判断某年份是否为闰年
function isLeapYear(year){
return year%4==0 && year%100!=0 || year%400==0;
}
//将日期格式化输出 “2015-08-24”
function date2string(date, sep){
var sep = sep || "-";
var m = date.getMonth()+1;
var d = date.getDate();
return date.getFullYear()+sep+ (m<10?"0"+m:m) + sep+ (d<10?"0"+d:d);
}
date2string(new Date())
//获得某个月份的天数
function getDaysByMonth(month, year){
year = year || new Date().getFullYear();
if(!month || typeof month != "number" ) {
console.error("参数必须为数字类型!");
return;
}
if( !(month > 0 && month < 13) ){
console.error("月份必须在1-12之间");
return;
}
month = Math.round(month);
switch(month) {
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 : return 31;
case 2 : return (isLeapYear(year) ? 29 : 28);
case 4 :
case 6 :
case 9 :
case 11 : return 30;
}
}
//将字符串转换为日期
function string2Date(datestr, sep){
if( !sep || !(datestr && datestr.length >= 8) ){
console.error("字符串格式错误!不能解析");
return;
}
var list = datestr.split(sep);
if( !(list[0].length==4 && list[1]>0&&list[1]<13 && list[2]>0 && list[2]<32) ){
console.error("字符串格式错误!不能解析");
return;
}
return new Date(datestr);
}
//判断两个日期相差的天数
function getDaysBetweenMonths(d1, d2){
if( !(d1 instanceof Date && d2 instanceof Date) ){
console.error("参数传错了!重来!");
return;
}
var dis = Math.abs(d1.getTime() - d2.getTime());
return (dis/1000/3600/24).toFixed(2);
}
//获得N天以后的日期(string/date)
function getAfterDay(n){
var now = new Date();
now.setDate( now.getDate()+n );
return date2string(now);
}