自定义jquery 常用方法。

$.format = function (source, params) {
    ///<summary>字符串格式化调用方法   <br />   var text = "a{0}b{0}c{1}d\nqq{0}";  <br />    var text2 = $.format(text, 1, 2); </summary>
    ///<param name="source" type="string">要格式化的字符串</param>
    ///<param name="params" type="动态值">要填充的值</param>

    if (arguments.length == 1)
    
        return function () {
            var args = $.makeArray(arguments);
            args.unshift(source);
            return $.format.apply(this, args);
    };
    
    if (arguments.length > 2 && params.constructor != Array) {
        
        params = $.makeArray(arguments).slice(1);
    };
    
    if (params.constructor != Array) {
        params = [params];
    }
    
    $.each(params, function (i, n) {
        source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
    });
    
    return source;
};

$.HTMLDecode = function(text) {
    var temp = document.createElement("div");
    temp.innerHTML = text;
    var output = temp.innerText || temp.textContent;
    temp = null;
    return output;
};

$.getScriptFolder = function() {
    ///<summary>获取当前js文件的路径</summary>
    var js = document.scripts;
    return js[js.length - 1].src.substring(0, js[js.length - 1].src.lastIndexOf("/") + 1);
};


$.genId = function() {
    ///<summary>模拟一串guid的数字</summary>

    var S4 = function() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };

    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
};


$.addTips = function(obj, message) {
    ///<summary>对控件添加提示</summary>
    ///<param name="obj" type="dom对象">要添加提示的对象</param>
    ///<param name="message" type="string">提示的信息</param>

    $(obj).addClass("error");
    $(obj).attr("title", message);
    $(obj).tooltip({
        position: "right",
        onShow: function() {
            $(this).tooltip('tip').css({
                'background-color': '#f9f2ba',
                'border': '1px solid #e9d315',
                'color': '#5b5316',
                'padding': '5px'
            });
        }
    });
};

$.removeTips = function(obj) {
    ///<summary>移除控件提示</summary>
    ///<param name="obj" type="dom对象">要移除提示的对象</param>

    if ($(obj).attr("class") && $(obj).attr("class").indexOf("error") > -1) {
        $(obj).tooltip("destroy");
    }

    $(obj).removeClass("error");
    $(obj).removeAttr("title");


};

//日期格式化
//调用  var d = new Date().format('yyyy-MM-dd');
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    };
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
                ("00" + o[k]).substr(("" + o[k]).length));
    return format;
};

//获取字符串的字节数
$.getByteLen = function(val) {
    var len = 0;
    for (var i = 0; i < val.length; i++) {
        if (val[i].match( /[^\x00-\xff]/ig ) != null) {
            len += 2;
        } else {
            len += 1;
        }
    }
    return len;
};


$.getByteVal = function (val, len, tips) {
    var reg = /[^\x00-\xff]/g, mid = Math.floor(len / 2), tips = tips || "...";
    if (val.replace(reg, "mm").length <= len) return val;
    for (var i = mid; i < val.length; i++) {
        if (val.substr(0, i).replace(reg, "mm").length >= len) {
            return val.substr(0, i) + tips;
        }
    }
    return val;
};


$.arrUnique = function (arr) {
    if (!arr || arr.constructor!==Array) return arr;
    var tempA = [], tempO = {};
    for (var i = 0, len = arr.length; i < len; i++) if (!tempO[arr[i]]) tempO[arr[i]] = 1;
    for (var j in tempO) tempA.push(j);
    return tempA;
};
//取url参数的值
$.getUrlParameter = function(key) {
    var par = location.search;
    var strReg = "[&|\\?]" + key + "=([^\&]*)";
    var reg = new RegExp(strReg, "i");
    if (reg.test(par)) {
        return RegExp.$1;
    }
    return "";
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值