zoukankan      html  css  js  c++  java
  • 常用js

    var util = {
    /**
    * 将json转换成字符串
    *
    * @param {JSON} data json对象
    */
    j2s: function(data){
    var that = this;
    try {
    return JSON.stringify(data);
    } catch(ex) {
    return "";
    }
    },
    /**
    * 将字符串转换成json
    *
    * @param {string} str 字符串
    */
    s2j: function(str){
    var that = this;
    try {
    return JSON.parse(str);
    } catch(ex) {
    return {};
    }
    },

    /**
    * 保存cookie
    *
    * @param {string} name 键名
    * @param {Object} value 值
    */
    setStorage: function(name, value) {
    $.cookie(name, value, { expires: 7, path:"/" });
    },
    /**
    * 读取cookie
    *
    * @param {string} name 键名
    */
    getStorage: function(name) {
    return $.cookie(name);
    },
    /**
    * 根据输入的密码,判断密码强度
    *
    * @param {Object} that 密码控件
    */
    checkPassword: function(that) {
    var maths, smalls, bigs, corps, cat, num, point;
    var str = $(that).val()
    var len = str.length;
    cat = /.*[u4e00-u9fa5]+.*$/
    if (cat.test(str)) return -1;
    cat = /d/;
    maths = cat.test(str);
    cat = /[a-z]/;
    smalls = cat.test(str);
    cat = /[A-Z]/;
    bigs = cat.test(str);
    corps = util.corpses($(that));
    num = maths + smalls + bigs + corps;

    if (len == 0) {
    point = 0;
    } else if (len <= 6) {
    point = 1;
    } else if (len >= 7 && len <= 9) {
    point = num == 1 || num == 2 ? 2 : 3;
    } else if (len >= 10 && len <= 13) {
    point = num == 1 || num == 2 ? 3 : 4;
    } else {
    return 4
    }

    return point;
    },
    corpses: function(that) {
    var cat = /./g
    var str = $(that).val();
    if (str.length > 0) {
    var sz = str.match(cat)
    for (var i = 0; i < sz.length; i++) {
    cat = /d/;
    maths_01 = cat.test(sz[i]);
    cat = /[a-z]/;
    smalls_01 = cat.test(sz[i]);
    cat = /[A-Z]/;
    bigs_01 = cat.test(sz[i]);
    if (!maths_01 && !smalls_01 && !bigs_01) {
    return true;
    } else {
    return false;
    }
    }
    } else {
    return false;
    }
    },
    /**
    * 限制文本框职能输入数字
    *
    * @param {Object} that 文本框控件
    */
    numOnly: function(that) {
    if ($(that).val() != undefined) {
    $(that).val($(that).val().replace(/[^0-9]/g, ''));
    }
    },
    /**
    * 验证是否是手机号
    *
    * @param {string} value 手机号
    */
    checkMobile: function(value) {
    try {
    return value.match(/^(13|14|15|17|18)d{9}$/);
    } catch(ex) {
    return false;
    }
    },
    /**
    * 验证是否是邮箱
    * @param {string} value 邮箱地址
    */
    checkEmail: function(value) {
    try {
    return value.match(/^w+((-w+)|(.w+))*@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+)*.[A-Za-z0-9]+$/);
    } catch(ex) {
    return false;
    }
    },
    /**
    * 将时间戳转换为时间显示
    * @param {float} unixTime 时间戳
    * @param {int} dateType 显示的时间类型:1.yyyy-MM-dd HH:mm; 2.MM-dd HH:mm; 3.yyyy-MM-dd; 4.MM-dd;
    * @param {int} size 尺寸
    */
    unixToDate1: function (datetime, dateType, size) {
    size = size || 1;
    if (datetime == undefined || datetime == null) return "";

    var unixTime, year, month, day, hours, minute, second, time;
    unixTime = new Date(datetime * size);
    year = unixTime.getFullYear();
    month = unixTime.getMonth() + 1 < 10 ? "0" + (unixTime.getMonth() + 1) : unixTime.getMonth() + 1;
    day = unixTime.getDate() < 10 ? "0" + unixTime.getDate() : unixTime.getDate();
    hours = unixTime.getHours() < 10 ? "0" + unixTime.getHours() : unixTime.getHours();
    minute = unixTime.getMinutes() < 10 ? "0" + unixTime.getMinutes() : unixTime.getMinutes();
    second = unixTime.getSeconds() < 10 ? "0" + unixTime.getSeconds() : unixTime.getSeconds();

    switch (dateType) {
    case 1: //yyyy-MM-dd HH:mm
    return year + "-" + month + "-" + day + " " + hours + ":" + minute;
    case 2: //MM-dd HH:mm
    return month + "-" + day + " " + hours + ":" + minute;
    case 3: //yyyy-MM-dd
    return year + "-" + month + "-" + day;
    case 4: //MM-dd
    return month + "-" + day;
    default: //yyyy-MM-dd HH:mm:ss
    return year + "-" + month + "-" + day + " " + hours + ":" + minute + ":" + second;
    }
    },
    /**
    * 将时间戳转换为时间显示
    * @param {float} unixTime 时间戳
    * @param {int} dateType 显示的事件类型:1.yyyy-MM-dd HH:mm:ss; 2.MM-dd HH:mm; 3.yyyy-MM-dd; 4.MM-dd;
    * @param {int} timeZone 时区
    */
    UnixToDate2: function(unixTime, dateType, timeZone) {
    var year, month, day, hours, minute, second, time;
    unixTime = typeof(timeZone) == 'number' ? parseInt(unixTime) + parseInt(timeZone) * 60 * 60 : unixTime;
    time = new Date(unixTime);
    year = time.getFullYear();
    month = time.getMonth() + 1;
    day = time.getDate();
    hours = time.getHours();
    minute = time.getMinutes();
    second = time.getSeconds();

    if (dateType == 1) {
    return year + "-" + month + "-" + day + " " + hours + ":" + minute + ":" + second;
    } else if (dateType == 2) {
    return month + "-" + day + " " + hours + ":" + minute;
    } else if (dateType == 3) {
    return year + "-" + month + "-" + day;
    } else if (dateType == 4) {
    return month + "-" + day;
    } else {
    return year + "-" + month + "-" + day + " " + hours + ":" + minute + ":" + second;
    }
    },
    /**
    * 日期 转换为 Unix时间戳
    * @param <string> 2014-01-01 20:20:20 日期格式
    * @return <int> unix时间戳(毫秒)
    */
    DateToUnix: function(string) {
    var f = string.split(' ', 2);
    var d = (f[0] ? f[0] : '').split('-', 3);
    var t = (f[1] ? f[1] : '').split(':', 3);
    return (new Date(
    parseInt(d[0], 10) || null,
    (parseInt(d[1], 10) || 1) - 1,
    parseInt(d[2], 10) || null,
    parseInt(t[0], 10) || null,
    parseInt(t[1], 10) || null,
    parseInt(t[2], 10) || null
    )).getTime();
    },
    /**
    * 根据条件,得到数组中的元素对象的索引
    * @param {Array} arr 数组
    * @param {string} key 对象的键名
    * @param {Object} value 对象的键值
    */
    arr_getIndex: function(arr, key, value) {
    var result = -1;
    $.grep(arr, function(cur, i) {
    if (cur[key] == value) {
    result = i;
    }
    });

    return result;
    },
    /**
    * 根据条件,得到数组中的元素对象
    * @param {Array} arr 数组
    * @param {String} key 对象的键名
    * @param {String} value 对象的键值
    */
    arr_getItem: function(arr, key, value) {
    var result = {};
    $.grep(arr, function(cur, i) {
    if (cur[key] == value) {
    result = cur;
    }
    });

    return result;
    },
    /**
    * 根据条件,删除数组中的元素对象
    * @param {Object} arr 数组
    * @param {string} key 对象的键名
    * @param {Object} value 对象的键值
    */
    arr_remove: function(arr, key, value) {
    return $.grep(arr, function(cur, i) {
    return cur[key] != value;
    });
    }

    }

    //页面跳转得到页面url的参数方法
    function gGetUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
    return unescape(r[2]);
    return null;
    }


    //操作缓存
    var historys = { //缓存
    isLocalStorage:window.localStorage?true:false,
    set : function(key,value){ //设置缓存
    value = JSON.stringify(value);
    if(this.isLocalStorage){
    window.localStorage.setItem(key,value);
    }else{
    var expireDays = 1; //失效时间
    var exDate=new Date();
    exDate.setTime(exDate.getTime()+expireDays*24*60*60*1000);
    document.cookie=key + "=" + escape(value)+";expires=" + exDate.toGMTString();
    }
    },
    get : function(key){ //读取缓存
    if(this.isLocalStorage){
    return JSON.parse(window.localStorage.getItem(key));
    }else{
    var arr,reg=new RegExp("(^| )"+key+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg)){
    return JSON.parse(unescape(arr[2]));
    }else{
    return null;
    }
    }
    },
    del : function(key){ //删除缓存
    if(this.isLocalStorage){
    localStorage.removeItem(key);
    }else{
    var exDate = new Date();
    exDate.setTime(exDate.getTime() - 1);
    var read_val=this.get(key);
    if(read_val!=null) document.cookie= key + "="+read_val+";expires="+exDate.toGMTString();
    }
    }
    }

    window.POST=function(url,params,func,header){//header不传默认前者,传就传true,代表后者
    var header = !header ? 'application/x-www-form-urlencoded;charset=utf-8' : 'application/json;charset=utf-8';
    $.ajax({
    type:"post",
    url:url,
    dataType:'json',
    async:false,
    contentType :header,
    data: params,
    success:function (response){
    if($.isFunction(func)){
    func(response);
    }
    },
    error:function(r){
    alert("返回信息有误");
    }
    });
    }
    function fCheckVal(type,value){
    if(type == 'phone'){
    return !!value.match(/^1[34578]d{9}$/g);
    }else if(type == 'name'){
    return !!value.match(/^[u4e00-u9fa5]{2,8}$/g);
    }else if(type == 'identity'){
    //return !!value.match(/^(d{15}$|^d{14}(d|X|x)$|^d{18}$|^d{17}(d|X|x))$/g);
    return checkIdcard(value);
    }else if(type == 'email'){
    return !!value.match(/^w+((-w+)|(.w+))*@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+)*.[A-Za-z0-9]+$/g);
    }else if(type == 'nums'){//数字,不含小数点
    return !!value.match(/[d]$/g);
    }else if(type == 'number'){//数值,含小数点
    return !!value.match(/^[+-]?d+(.d+)?$/g);// /[d.]$/g
    }else if(type == 'tel'){
    return !!/^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$/.test(value);
    }else if(type == 'tOrP'){//手机号或座机号
    return !!/^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$|^1[34578]d{9}$/.test(value);
    }else if(type == 'qq'){
    return RegExp(/^[1-9][0-9]{4,10}$/).test(value);
    }else if(type == 'bankCard'){
    return !!value.match(/^[12346789]d{15}$|^[12346789]d{18}$|^[12346789]d{16}$|^[12346789]d{17}$/g);
    }else if(type == 'nOrW'){//字母和数字
    return RegExp(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{2,20}$/).test(value);
    }else if(type == 'nw'){//字母或数字
    return !!value.match(/^[A-Za-z0-9]+$/g);
    }else if(type == 'pCode'){//邮编
    return !!value.match(/^d{6}$/g);
    }else{
    return true;
    }
    }


    var htmlAtt = [];
    for (var i = 0, len = data.length; i < len; i++) {
    var html='<a style="color:black" class="weui_cell weui_check_label upload" href='+htmlHref+'>
    <div class="weui_cell_hd">
    <input type="checkbox" class="weui_check" name="checkbox" disabled="disabled" '+ (documentID ? "checked=checked" : "")+' />
    <i class="weui_icon_checked"></i>
    </div>
    <div class="weui_cell_bd weui_cell_primary">
    <span class="print-apply-form">
    '+typename+'<font color="red"></font>
    </span>
    '+(documentID ? '<img src="'+ src +'" class="icon-camera icon" />' : '<span class="icon-camera icon"></span>')+'
    </div>
    </a>';
    htmlAtt.push(html);
    }
    $("div.weui_cells_checkbox").html(htmlAtt.join(""));

    $("div.weui_dialog_alert").show().find("strong.weui_dialog_title").text("保存成功").end().find("a.weui_btn_dialog").on("click",function(){
    location.href = "my-advanced.html";
    });

  • 相关阅读:
    StrUtils单元中的函数
    windows系统字体问题
    进程间通讯(模拟远程CMD)
    sql远程连接SQL(不能打开到主机的连接,在端口 1433:连接失败)
    在delphi中三个形式:ADODB_TLB ADOInt ADODB
    安装Ms SQL Server时提示:以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机。
    从Indy9升级到Indy10时IdTcpServer的变化
    如何实现调用DOS程序的时候的管道输入重定向!
    接口技术
    SysUtils单元函数
  • 原文地址:https://www.cnblogs.com/kekang/p/6131282.html
Copyright © 2011-2022 走看看