Jquery扩展方法

自己可能会用到的一些js方法,扩展到jquery里面吧,做一个自己用的jquery公共扩展库,持续更新中。。。

//字符串参数化替换函数
$.common = {
    format: function () {
        if (arguments.length == 0)
            return null;
        var str = arguments[0];
        for (var i = 1; i < arguments.length; i++) {
            var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
            str = str.replace(re, arguments[i]);
        }
        return str;
    },
    //获取url参数
    getUrlParam: function (name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    },
    //将秒数转换为hh:mm:ss的格式
    formatSeconds: function (value) {
        var totalSec = parseInt(value);//
        var sec = 0;//
        var min = 0;//
        var hour = 0;// 小时
        if (totalSec > 60) {
            min = parseInt(totalSec / 60);
            sec = parseInt(totalSec % 60);
            if (min > 60) {
                hour = parseInt(min / 60);
                min = parseInt(min % 60);
            }
        }
        else {
            sec = totalSec;
        }
        hour = hour >= 10 ? hour.toString() : "0" + hour.toString();
        min = min >= 10 ? min.toString() : "0" + min.toString();
        sec = sec >= 10 ? sec.toString() : "0" + sec.toString();
        return hour + ":" + min + ":" + sec;
    },
    //计算文件大小(传入单位为B)
    FormateFileSize: function (size) {
        var fileSize = parseFloat(size) / 1024;
        var unit = "K";
        if (fileSize > 1024) {
            fileSize = fileSize / 1024;
            unit = "M";
        }
        if (fileSize > 1024) {
            fileSize = fileSize / 1024;
            unit = "G";
        }
        return fileSize.toFixed(2) + unit;
    },
    //生成guid
    guid: function () {
        function S4() {
            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        }
        return (S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4());
    }
};

 

posted @ 2015-12-11 15:53  yyq745201  阅读(247)  评论(0编辑  收藏  举报