記錄JavaScript的util.js類庫
阿新 • • 發佈:2018-07-05
block 隨機數 and max 需要 .html lis pty 彈出
工作中用到的, 不斷做為積累, 以後能用到. 也感謝前輩們.
定義Util對象
var MyUtil = new Object();
從url中獲取參數
//從url中獲取參數 function GetQueryString(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if(name == ‘qu_name‘){ if(r!=null)returndecodeURI(r[2]); return null; }else{ if(r!=null)return unescape(r[2]); return null; } }
生成32位隨機數
// 生成32位隨機數 function randomString() { /****默認去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/ var $chars = ‘ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678‘; var maxPos = $chars.length;var pwd = ‘‘; for (var i = 0; i < 32; i++) { pwd += $chars.charAt(Math.floor(Math.random() * maxPos)); } return pwd; }
隱藏DIV
MyUtil.hideDiv = function(div) { div.css("display", "none"); };
彈出div顯示層
/** * 彈出div顯示層 * * @param {} * div 需要顯示的div元素 * @param {} * content div中顯示的內容 * @param {} * left 左偏移量 * @param {} * top 頂部偏移量 * @param {} * width div寬度 * @param {} * height div高度*/ MyUtil.showDiv = function(div, content, left, top, width, height) { div.empty(); div.append(content); HuaDun.setPosition(div, left, top); if (typeof width != ‘undefined‘ && (width != null || width != "")) { div.css("width", width + "px"); } if (typeof height != ‘undefined‘ && (height != null || height != "")) { div.css("height", height + "px"); } div.css("display", "block"); };
設置彈出div位置
/** * 設置彈出div位置 * * @param {} * div 需要顯示的div元素 * @param {} * x 左偏移量 * @param {} * y 頂部偏移量 */ MyUtil.setPosition = function(div, x, y) { var top = document.body.scrollTop || document.documentElement.scrollTop; var left = document.body.scrollLeft || document.documentElement.scrollLeft; x += left; y += top; var l = x + 20;// 左偏移量 var t = y - (div.height() - 20); // 頂部偏移量 var bRight = true; var iPageRight = left + document.documentElement.clientWidth;// 頁面的右邊界 if (l + div.width() > iPageRight) { bRight = false; l = x - (div.width() + 20); } div.css("left", l + ‘px‘); div.css("top", t + ‘px‘); }
時間工具類
/** * 時間類工具 */ var MyDate= new Object(); // 獲取當前時間 MyDate.GetNow = function() { var now = new Date(); var year = now.getFullYear(); var month = now.getMonth() + 1; var day = now.getDate(); var hh = now.getHours(); // 獲取當前小時數(0-23) var mm = now.getMinutes(); // 獲取當前分鐘數(0-59) var ss = now.getSeconds(); var clock = year + "-"; if (month < 10) clock += "0"; clock += month + "-"; if (day < 10) clock += "0"; clock += day; var h = parseInt(hh); if (h < 10) { h = "0" + h; } clock += " " + h; var m = parseInt(mm); if (m < 10) { m = "0" + m; } clock += ":" + m; var s = parseInt(ss); if (s < 10) { s = "0" + s; } clock += ":" + s; return (clock); }; // 獲取當天日期 MyDate.GetToday = function() { var now = new Date(); var year = now.getFullYear(); // 年 var month = now.getMonth() + 1; // 月 var day = now.getDate(); // 日 var clock = year + "-"; if (month < 10) clock += "0"; clock += month + "-"; if (day < 10) clock += "0"; clock += day; return (clock); }; // 獲取昨天日期 MyDate.GetYesterday = function() { var date = new Date(); var yesterday_milliseconds = date.getTime() - 1000 * 60 * 60 * 24; var yesterday = new Date(); yesterday.setTime(yesterday_milliseconds); var strYear = yesterday.getFullYear(); var strDay = yesterday.getDate(); if(strDay < 10){ strDay = "0"+strDay; } var strMonth = yesterday.getMonth() + 1; if (strMonth < 10) { strMonth = "0" + strMonth; } datastr = strYear + "-" + strMonth + "-" + strDay; return datastr; }; // 獲取明天日期 MyDate.GetTomorrow = function() { var date = new Date(); var Tomorrow_milliseconds = date.getTime() + 1000 * 60 * 60 * 24; var Tomorrow = new Date(); Tomorrow.setTime(Tomorrow_milliseconds); var strYear = Tomorrow.getFullYear(); var strDay = Tomorrow.getDate(); if(strDay < 10){ strDay = "0"+strDay; } var strMonth = Tomorrow.getMonth() + 1; if (strMonth < 10) { strMonth = "0" + strMonth; } datastr = strYear + "-" + strMonth + "-" + strDay; return datastr; }; // 獲取當前年 MyDate.GetYear = function() { var y = new Date(); var year = y.getFullYear(); return year; }; // 獲取當前月 MyDate.GetMonth = function() { var m = new Date(); var year = m.getFullYear(); var month = m.getMonth() + 1; if (month < 10) { month = "0" + month; } return year + "-" + month; }; // 獲取上個月 MyDate.GetLastMonth = function() { var tomonth = new Date(); var year = tomonth.getFullYear(); // 獲取當前日期的年 var month = tomonth.getMonth() + 1; // 獲取當前日期的月 var year2 = year; var month2 = parseInt(month) - 1; if (month2 == 0) { year2 = parseInt(year2) - 1; month2 = 12; } if (month2 < 10) { month2 = ‘0‘ + month2; } var lastmonth = year2 + ‘-‘ + month2; return lastmonth; }; // 獲取下個月 MyDate.GetNextMonth = function() { var tomonth = new Date(); var year = tomonth.getFullYear(); // 獲取當前日期的年 var month = tomonth.getMonth() + 1; // 獲取當前日期的月 var year2 = year; var month2 = parseInt(month) + 1; if (month2 == 13) { year2 = parseInt(year2) + 1; month2 = 1; } if (month2 < 10) { month2 = ‘0‘ + month2; } var nextmonth = year2 + ‘-‘ + month2; return nextmonth; };
自動填充表單
/** * 華盾自動填充表單 * * @param {} * customForm 表單Form元素 * @param {} * data 要填充的數據對象 */ MyUtil.AutoFillForm = function(customForm, data) { for (var attr in data) { if (typeof(data[attr]) == ‘function‘) { continue; } var $input = $("input[name=‘" + attr + "‘]", customForm); if (null != $input && $input.length > 0) { var type = $input.attr("type"); if (type == "checkbox" || type == "radio") { var avalues = data[attr].split(","); for (var v = 0; v < avalues.length; v++) { $input.each(function(i, n) { var value = $(n).val(); if (value == avalues[v]) { $(n).attr("checked", "checked"); } }); } } else { $input.val(data[attr]); } } else { var $select = $("select[name=‘" + attr + "‘]", customForm); if (null != $select && $select.length > 0) { $select.val(data[attr]); } else { var $textarea = $("textarea[name=‘" + attr + "‘]", customForm); if (null != $textarea && $textarea.length > 0) { $textarea.val(data[attr]); } else { var $span = $("span[name=‘" + attr + "‘]", customForm); if (null != $span && $span.length > 0) { $span.html(data[attr]); }else{ continue; } } } } } }
記錄JavaScript的util.js類庫