jquery封装的方法

寻技术 JQuery 2023年09月01日 61
<!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:;
                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:;
                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() +  // 月
                var day = now.getDate() // 日
                var hh = now.getHours() // 时
                var mm = now.getMinutes() // 分
                var clock = year + '-'
                if(month < ) {
                    clock += ''
                }
                clock += month + '-'
                if(day < ) {
                    clock += ''
                }
                clock += day + ' '
                if(hh < ) {
                    clock += ''
                }
                clock += hh + ':'
                if(mm < ) clock += ''
                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() === ) week = '星期日'
                if(timedat.getDay() === ) week = '星期一'
                if(timedat.getDay() === ) week = '星期二'
                if(timedat.getDay() === ) week = '星期三'
                if(timedat.getDay() === ) week = '星期四'
                if(timedat.getDay() === ) week = '星期五'
                if(timedat.getDay() === ) 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 > ) {
                    new_mobile = mobile.substr(, ) + '****' + mobile.substr()
                }
                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 === ) {
                    if(!regName.test(num)) return false
                } else if(type === ) {
                    regName = /[^\d]/g
                    if(!regName.test(num)) return false
                }
                return true
            }
            //判断是否为小数
            function isNumOrd(num, type) {
                let regName = /[^\d.]/g
                if(type === ) {
                    if(!regName.test(num)) return false
                } else if(type === ) {
                    regName = /[^\d.]/g
                    if(!regName.test(num)) return false
                }
                return true
            }
            //判断是否为空
            function isNull(val) {
                if(val instanceof Array) {
                    if(val.length === ) 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 = {
                    : '北京',
                    : '天津',
                    : '河北',
                    : '山西',
                    : '内蒙古',
                    : '辽宁',
                    : '吉林',
                    : '黑龙江 ',
                    : '上海',
                    : '江苏',
                    : '浙江',
                    : '安徽',
                    : '福建',
                    : '江西',
                    : '山东',
                    : '河南',
                    : '湖北 ',
                    : '湖南',
                    : '广东',
                    : '广西',
                    : '海南',
                    : '重庆',
                    : '四川',
                    : '贵州',
                    : '云南',
                    : '西藏 ',
                    : '陕西',
                    : '甘肃',
                    : '青海',
                    : '宁夏',
                    : '新疆',
                    : '台湾',
                    : '香港',
                    : '澳门',
                    : '国外 '
                }
                if(!isNull(code)) {
                    if(code.length === ) {
                        if(!code || !/(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(code)) {
                            msg = '证件号码格式错误'
                            return false
                        } else if(!city[code.substr(, )]) {
                            msg = '地址编码错误'
                            return false
                        } else {
                            // 18位身份证需要验证最后一位校验位
                            code = code.split('')
                            // ∑(ai×Wi)(mod 11)
                            // 加权因子
                            let factor = [, , , , , , , , , , , , , , , , ]
                            // 校验位
                            let parity = [, , 'X', , , , , , , , , 'x']
                            let sum = 
                            let ai = 
                            let wi = 
                            for(let i = ; i < ; i++) {
                                ai = code[i]
                                wi = factor[i]
                                sum += ai * wi
                            }
                            let last = parity[sum % ]
                            if(last !== code[]) {
                                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 ==  ? '' : '') + num;
                }

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

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

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

                if(timestampDiff < ) { // 一分钟以内
                    return '刚刚';
                } else if(timestampDiff < ) { // 一小时前之内
                    return Math.floor(timestampDiff / ) + '分钟前';
                } else if(curDate.getFullYear() == Y && curDate.getMonth() +  == m && curDate.getDate() == d) {
                    return '今天' + zeroize(H) + ':' + zeroize(i);
                } else {
                    var newDate = new Date((curTimestamp - ) * ); // 参数中的时间戳加一天转换成的日期对象
                    if(newDate.getFullYear() == Y && newDate.getMonth() +  == 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() +  <  ? '' + (date.getMonth() + ) : date.getMonth() + ) + '-';
                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() + , //月份
                    "d+": date.getDate(), //日
                    "h+": date.getHours(), //小时
                    "m+": date.getMinutes(), //分
                    "s+": date.getSeconds(), //秒
                    "q+": Math.floor((date.getMonth() + ) / ), //季度
                    "S": date.getMilliseconds() //毫秒
                };
                if(/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$, (date.getFullYear() + "").substr( - RegExp.$.length));
                for(var k in o)
                    if(new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$, (RegExp.$.length == ) ? (o[k]) : (("" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            }

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

            //   * 判断数组是否为空
            function arrayIsEmpty(value) {
                return(Array.isArray(value) && value.length === ) || (Object.prototype.isPrototypeOf(value) && Object.keys(value).length === );
            }
            //   * @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,给予处理
                }
                if((lng2 == null || lng2 == '') && (lat2 == null || lat2 == '')) {
                    return ; // 如果定位失败的话,传入的是两个空值,并且返回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 =  * Math.asin(Math.sqrt(Math.pow(Math.sin(a / ), ) +
                    Math.cos(radLat1) * Math.cos(radLat2) *
                    Math.pow(Math.sin(b / ), )));
                s = s * EARTH_RADIUS;
                // s = Math.round(s * 10000) / 10000; // 精确到小数点后四位
                s = (Math.round(s * ) / ).toFixed(); // 保留两位小数
                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 * ) / ;
                var s = f.toString();
                var rs = s.indexOf('.');
                if(rs < ) {
                    rs = s.length;
                    s += '.';
                }
                while(s.length <= rs + ) {
                    s += '';
                }
                return s;
            }

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

        })
    </script>

</html>

 jquery封装的常用方法

如需转载请联系原作者
如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:yqgroup@service.aliyun.com 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
关闭

用微信“扫一扫”