有志者事竟成。

jquery封装的方法

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>一些常用的方法</title>
        <style type="text/css">
            p {
                overflow: hidden;
                /*超出部分隐藏*/
                text-overflow: ellipsis;
                /* 超出部分显示省略号 */
                white-space: nowrap;
                /*规定段落中的文本不进行换行 */
                width: 250px;
                /*需要配合宽度来使用*/
                border: 1px solid red;
                font-size: 30px;
            }
            
            P {
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 3;
                overflow: hidden;
                width: 250px;
                border: 1px solid red;
                font-size: 30px;
            }
            
            p {
                position: relative;
                line-height: 20px;
                /*可变*/
                max-height: 80px;
                /*可变*/
                overflow: hidden;
                width: 240px;
                /*可变*/
                border: 1px solid red;
            }
            
            p::after {
                content: "...";
                position: absolute;
                bottom: 0;
                right: 7px;
                /*可变*/
                padding-right: 124px;
                /*兼容性处理*/
                background: -webkit-linear-gradient(left, transparent, #fff 0%);
                background: -o-linear-gradient(right, transparent, #fff 0%);
                background: -moz-linear-gradient(right, transparent, #fff 0%);
                background: linear-gradient(to right, transparent, #fff 0%);
            }
        </style>

    </head>

    <body>
        <label></label>
    </body>
    <script src="jquery-2.1.4.min.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
        $(function() {

            //            时间搓转年月日   例子:console.log(curentTime('15000000000'))
            function curentTime(time) {
                var now = new Date(time)
                var year = now.getFullYear() //
                var month = now.getMonth() + 1 //
                var day = now.getDate() //
                var hh = now.getHours() //
                var mm = now.getMinutes() //
                var clock = year + '-'
                if(month < 10) {
                    clock += '0'
                }
                clock += month + '-'
                if(day < 10) {
                    clock += '0'
                }
                clock += day + ' '
                if(hh < 10) {
                    clock += '0'
                }
                clock += hh + ':'
                if(mm < 10) clock += '0'
                clock += mm
                return(clock)
            }

            // 根据时间判断星期几   例子:console.log(getWeek(new Date("2017-10-27" )))
            function getWeek(timedat) { // timedat参数格式:   getWeek(new Date("2017-10-27" ))
                var week
                if(timedat.getDay() === 0) week = '星期日'
                if(timedat.getDay() === 1) week = '星期一'
                if(timedat.getDay() === 2) week = '星期二'
                if(timedat.getDay() === 3) week = '星期三'
                if(timedat.getDay() === 4) week = '星期四'
                if(timedat.getDay() === 5) week = '星期五'
                if(timedat.getDay() === 6) week = '星期六'
                return week
            }

            //            邮箱邮箱验证   true  false  例子:console.log(isEmail('508744736@qq.com'))
            function isEmail(s) {
                return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)
            }
            //            手机号码
            function isMobile(s) {
                return /^1[0-9]{10}$/.test(s)
            }
            //            电话号码
            function isPhone(s) {
                return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s)
            }
            //            隐藏手机号   例子:console.log(hideMobile('13148238727'))
            function hideMobile(mobile) {
                var new_mobile = mobile.length
                if(mobile.length > 7) {
                    new_mobile = mobile.substr(0, 3) + '****' + mobile.substr(7)
                }
                return new_mobile
            }
            //URL地址
            function isURL(s) {
                return /^http[s]?:\/\/.*/.test(s)
            }
            //小写字母
            function isLowerCase(str) {
                const reg = /^[a-z]+$/
                return reg.test(str)
            }
            //大写字母
            function isUpperCase(str) {
                const reg = /^[A-Z]+$/
                return reg.test(str)
            }
            //大小写字母
            function isAlphabets(str) {
                const reg = /^[A-Za-z]+$/
                return reg.test(str)
            }
            //            判断姓名是否正确
            function isName(name) {
                let regName = /^[\u4e00-\u9fa5]{2,4}$/
                if(!regName.test(name)) return false
                return true
            }
            //判断是否为整数
            function isNum(num, type) {
                let regName = /[^\d.]/g
                if(type === 1) {
                    if(!regName.test(num)) return false
                } else if(type === 2) {
                    regName = /[^\d]/g
                    if(!regName.test(num)) return false
                }
                return true
            }
            //判断是否为小数
            function isNumOrd(num, type) {
                let regName = /[^\d.]/g
                if(type === 1) {
                    if(!regName.test(num)) return false
                } else if(type === 2) {
                    regName = /[^\d.]/g
                    if(!regName.test(num)) return false
                }
                return true
            }
            //判断是否为空
            function isNull(val) {
                if(val instanceof Array) {
                    if(val.length === 0) return true
                } else if(val instanceof Object) {
                    if(JSON.stringify(val) === '{}') return true
                } else {
                    if(val === 'null' || val == null || val === 'undefined' || val === undefined || val === '') return true
                    return false
                }
                return false
            }
            //判断身份证号码
            function isCardId(code) {
                let msg = ''
                const city = {
                    11: '北京',
                    12: '天津',
                    13: '河北',
                    14: '山西',
                    15: '内蒙古',
                    21: '辽宁',
                    22: '吉林',
                    23: '黑龙江 ',
                    31: '上海',
                    32: '江苏',
                    33: '浙江',
                    34: '安徽',
                    35: '福建',
                    36: '江西',
                    37: '山东',
                    41: '河南',
                    42: '湖北 ',
                    43: '湖南',
                    44: '广东',
                    45: '广西',
                    46: '海南',
                    50: '重庆',
                    51: '四川',
                    52: '贵州',
                    53: '云南',
                    54: '西藏 ',
                    61: '陕西',
                    62: '甘肃',
                    63: '青海',
                    64: '宁夏',
                    65: '新疆',
                    71: '台湾',
                    81: '香港',
                    82: '澳门',
                    91: '国外 '
                }
                if(!isNull(code)) {
                    if(code.length === 18) {
                        if(!code || !/(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(code)) {
                            msg = '证件号码格式错误'
                            return false
                        } else if(!city[code.substr(0, 2)]) {
                            msg = '地址编码错误'
                            return false
                        } else {
                            // 18位身份证需要验证最后一位校验位
                            code = code.split('')
                            // ∑(ai×Wi)(mod 11)
                            // 加权因子
                            let factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
                            // 校验位
                            let parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2, 'x']
                            let sum = 0
                            let ai = 0
                            let wi = 0
                            for(let i = 0; i < 17; i++) {
                                ai = code[i]
                                wi = factor[i]
                                sum += ai * wi
                            }
                            let last = parity[sum % 11]
                            if(last !== code[17]) {
                                msg = '证件号码校验位错误'
                                return false
                            }
                        }
                    } else {
                        msg = '证件号码长度不为18位'
                        return false
                    }
                } else {
                    msg = '证件号码不能为空'
                    return false
                }
                if(msg) {
                    console.log(msg)
                }
                return true
            }

            //            去掉字符串前后所有空格
            function trim(str) {
                return str.replace(/(^\s*)|(\s*$)/g, "");
            }
            //            格式化时间戳  console.log(timestampFormat(150000000000))
            function timestampFormat(timestamp) {
                function zeroize(num) {
                    return(String(num).length == 1 ? '0' : '') + num;
                }

                var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
                var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数

                var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
                var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象

                var Y = tmDate.getFullYear(),
                    m = tmDate.getMonth() + 1,
                    d = tmDate.getDate();
                var H = tmDate.getHours(),
                    i = tmDate.getMinutes(),
                    s = tmDate.getSeconds();

                if(timestampDiff < 60) { // 一分钟以内
                    return '刚刚';
                } else if(timestampDiff < 3600) { // 一小时前之内
                    return Math.floor(timestampDiff / 60) + '分钟前';
                } else if(curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) {
                    return '今天' + zeroize(H) + ':' + zeroize(i);
                } else {
                    var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
                    if(newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) {
                        return '昨天' + zeroize(H) + ':' + zeroize(i);
                    } else if(curDate.getFullYear() == Y) {
                        return zeroize(m) + '-' + zeroize(d) + ' ' + zeroize(H) + ':' + zeroize(i);
                    } else {
                        return Y + '-' + zeroize(m) + '-' + zeroize(d) + ' ' + zeroize(H) + ':' + zeroize(i);
                    }
                }
            }

            //将时间戳转换成时间格式  console.log(timestampToTime(150000000000000))
            function timestampToTime(timestamp) {
                var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
                var Y = date.getFullYear() + '-';
                var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
                var D = date.getDate() + ' ';
                var h = date.getHours() + ':';
                var m = date.getMinutes()
                // + ':';
                // var s = date.getSeconds();
                return Y + M + D + h + m;
            }
            //     * 对Date的扩展,将 Date 转化为指定格式的String
            //   * 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符
            //   * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
            //   * (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
            //   * (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
            //   * @param {Date} date 日期类型
            //   * @param {string} fmt 格式化的形式
            function dateFormat(date, fmt) {
                var o = {
                    "M+": date.getMonth() + 1, //月份
                    "d+": date.getDate(), //
                    "h+": date.getHours(), //小时
                    "m+": date.getMinutes(), //
                    "s+": date.getSeconds(), //
                    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                    "S": date.getMilliseconds() //毫秒
                };
                if(/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
                for(var k in o)
                    if(new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            }

            //             * 当前时间 day 天后的年月日时分秒
            function fun_date(day) {
                var date1 = new Date(),
                    time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate(); //time1表示当前时间
                var date2 = new Date(date1);
                date2.setDate(date1.getDate() + day);
                var time2 = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
                return time2;
            }

            //   * 判断数组是否为空
            function arrayIsEmpty(value) {
                return(Array.isArray(value) && value.length === 0) || (Object.prototype.isPrototypeOf(value) && Object.keys(value).length === 0);
            }
            //   * @description 判断字符是否为空
            function charisEmpty(obj) {
                if(typeof obj == 'undefined' || obj == null || obj == '') {
                    return true;
                } else {
                    return false;
                }
            }
            //  * @description 将用角度表示的角转换为近似相等的用弧度表示的角 java Math.toRadians
            function rad(d) {
                return d * Math.PI / 180.0;
            }

            //   * @description 谷歌地图计算两个坐标点的距离
            //   * @param {number} lng1  经度1
            //   * @param {number} lat1  纬度1
            //   * @param {number} lng2  经度2
            //   * @param {number} lat2  纬度2
            //   * @return 距离(km) [注意:如果定位失败的话,传入的是两个空值,并且返回0,给予处理]
            function getDistance(lng1, lat1, lng2, lat2) {
                if(lng1 == '' && lat1 == '') {
                    return 0; // 如果定位失败的话,传入的是两个空值,并且返回0,给予处理
                }
                if((lng2 == null || lng2 == '') && (lat2 == null || lat2 == '')) {
                    return 0; // 如果定位失败的话,传入的是两个空值,并且返回0,给予处理
                }
                var radLat1 = this.rad(lat1);
                var radLat2 = this.rad(lat2);
                var a = radLat1 - radLat2;
                var b = this.rad(lng1) - this.rad(lng2);
                var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                    Math.cos(radLat1) * Math.cos(radLat2) *
                    Math.pow(Math.sin(b / 2), 2)));
                s = s * EARTH_RADIUS;
                // s = Math.round(s * 10000) / 10000; // 精确到小数点后四位
                s = (Math.round(s * 10000) / 10000).toFixed(2); // 保留两位小数
                return s;
            }

            //制保留2位小数,如:2,会在2后面补上00.即2.00
            function toDecimal(x) {
                var f = parseFloat(x);
                if(isNaN(f)) {
                    return false;
                }
                var f = Math.round(x * 100) / 100;
                var s = f.toString();
                var rs = s.indexOf('.');
                if(rs < 0) {
                    rs = s.length;
                    s += '.';
                }
                while(s.length <= rs + 2) {
                    s += '0';
                }
                return s;
            }

            // 时间格式化输出,如11:03 25:19 每1s都会调用一次
            function dateformat(micro_second, t = 0) {
                // 总秒数
                var second = Math.floor(micro_second / 1000);
                // 天数
                var day = Math.floor(second / 3600 / 24);
                // 小时
                var hr = Math.floor(second / 3600 % 24);
                // 分钟
                var min = Math.floor(second / 60 % 60);
                //
                var sec = Math.floor(second % 60);
                if(t == 0) {
                    return day + ":" + hr + ":" + min + ":" + sec;
                } else {
                    return "0" + ":" + hr + ":" + min + ":" + sec;
                }
                // if (t == 0) {
                //   return day + "天" + hr + "时" + min + "分" + sec + "秒";
                // } else {
                //   return hr + "时" + min + "分" + sec + "秒";
                // }
            }
            
            
            

        })
    </script>

</html>

 jquery封装的常用方法

原文发布时间为:2019年08月28日
原文作者:李鹏泽
如需转载请联系原作者

posted on 2019-08-28 19:28  阿长*长  阅读(1453)  评论(0编辑  收藏  举报

导航