代码片段

前端开发常用的代码片段

时间格式化

Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.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;
}

//to use
new Date(timer).format("yyyy-MM-dd hh:mm:ss")

手机号码验证

身份证验证

获取URL中的参数

随机数生成

对象深拷贝

清除对象中值为空的属性

获取文件名后缀

冒泡排序

最简单的模板引擎

去除空格

字符串替换

Base64转图片

文本溢出显示省略号

JS去除空格

清除对象中值为空的属性

获取文件后缀名

去除字符串中的空格

转xml

最后更新于