码迷,mamicode.com
首页 > 编程语言 > 详细

记录JavaScript的util.js类库

时间:2018-07-05 01:26:02      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:string   tar   cat   工具   nbsp   mon   efi   checked   abc   

工作中用到的, 不断做为积累, 以后能用到. 也感谢前辈们. 

定义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)return  decodeURI(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类库

标签:string   tar   cat   工具   nbsp   mon   efi   checked   abc   

原文地址:https://www.cnblogs.com/embraceU/p/9266138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!