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);
}
}
//計(jì)算一個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
};
}
//封裝事件監(jiān)聽的添加
function addEventHandler(ele, eventType, fn, isCapture){
if(ele.addEventListener) {
ele.addEventListener(eventType, fn, isCapture);
} else {
ele.attachEvent("on"+eventType, fn);
}
}
//限定一個數(shù)字的大小范圍
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(現(xiàn)在經(jīng)常使用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())
//獲得某個月份的天數(shù)
function getDaysByMonth(month, year){
year = year || new Date().getFullYear();
if(!month || typeof month != "number" ) {
console.error("參數(shù)必須為數(shù)字類型!");
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;
}
}
//將字符串轉(zhuǎn)換為日期
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);
}
//判斷兩個日期相差的天數(shù)
function getDaysBetweenMonths(d1, d2){
if( !(d1 instanceof Date && d2 instanceof Date) ){
console.error("參數(shù)傳錯了晰洒!重來!");
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);
}